Compare commits
2 commits
3d4d07ba88
...
9d0371d930
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d0371d930 | ||
|
|
d5b2e3c2e8 |
|
|
@ -1,404 +0,0 @@
|
|||
Question 01: Write a Python program to accept two integers and print their sum.
|
||||
|
||||
Answer:
|
||||
def main(int1: int, int2: int) -> int:
|
||||
print(f"{int1} + {int2} = {int1 + int2}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2, 2)
|
||||
|
||||
|
||||
Output:
|
||||
2 + 2 = 4
|
||||
|
||||
|
||||
Question 02: Write a Python program that accepts the radius of a circle and prints its area.
|
||||
|
||||
Answer:
|
||||
def main(radius: int):
|
||||
print(f"Area: {3.14 * radius * radius}")
|
||||
# or you can do 3.13 * radius ** 2
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(7)
|
||||
|
||||
|
||||
Output:
|
||||
Area: 153.86
|
||||
|
||||
|
||||
Question 03: Write a Python program to accept the length and width of a rectangle and compute its perimeter and area.
|
||||
|
||||
Answer:
|
||||
def main(length: int, width: int) -> None:
|
||||
print("Perimeter:", 2 * (length + width))
|
||||
print("Area:", length * width)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 10)
|
||||
|
||||
|
||||
Output:
|
||||
Perimeter: 40
|
||||
Area: 100
|
||||
|
||||
|
||||
Question 04: Write a Python program to compute simple interest for a given principal amount, time, and rate of interest.
|
||||
|
||||
Answer:
|
||||
def main(principal: int, time: int, rate: float) -> None:
|
||||
print(f"Simple Interest: {principal * rate * time / 100}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10000, 10, 5.0)
|
||||
|
||||
|
||||
Output:
|
||||
Simple Interest: 5000.0
|
||||
|
||||
|
||||
Question 05: Write a Python program to find whether a given number is even or odd.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
print("Even" if num % 2 == 0 else "Odd")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2)
|
||||
main(3)
|
||||
|
||||
|
||||
Output:
|
||||
Even
|
||||
Odd
|
||||
|
||||
|
||||
Question 06: Write a Python program to find the largest among three numbers.
|
||||
|
||||
Answer:
|
||||
def main(numbers: list[int]) -> None:
|
||||
if not numbers:
|
||||
print("No numbers provided.")
|
||||
return
|
||||
|
||||
largest = numbers[0]
|
||||
for num in numbers:
|
||||
if num > largest:
|
||||
largest = num
|
||||
|
||||
print("Largest number:", largest)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main([1, 2, 3])
|
||||
main([3, 1, 2])
|
||||
main([5, 10, 15, 20])
|
||||
|
||||
|
||||
Output:
|
||||
Largest number: 3
|
||||
Largest number: 3
|
||||
Largest number: 20
|
||||
|
||||
|
||||
Question 07: Write a Python program to perform arithmetic calculation. The program should accept two operands and an operator, then display the result.
|
||||
|
||||
Answer:
|
||||
def main(num1: float, num2: float, operator: str) -> None:
|
||||
if operator == '+':
|
||||
result = num1 + num2
|
||||
elif operator == '-':
|
||||
result = num1 - num2
|
||||
elif operator == '*':
|
||||
result = num1 * num2
|
||||
elif operator == '/':
|
||||
if num2 == 0:
|
||||
print("Error: Division by zero")
|
||||
raise ZeroDivisionError
|
||||
result = num1 / num2
|
||||
else:
|
||||
print("Error: Invalid operator")
|
||||
raise ValueError("Invalid operator. Use +, -, *, or /.")
|
||||
|
||||
print(f"Result: {result}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 5, '+')
|
||||
main(10, 5, '-')
|
||||
main(10, 5, '*')
|
||||
main(10, 5, '/')
|
||||
|
||||
|
||||
Output:
|
||||
Result: 15
|
||||
Result: 5
|
||||
Result: 50
|
||||
Result: 2.0
|
||||
|
||||
|
||||
Question 08: Write a Python program to check whether a given year is a leap year or not.
|
||||
|
||||
Answer:
|
||||
def main(year: int) -> None:
|
||||
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
|
||||
print(f"{year} is a leap year.")
|
||||
else:
|
||||
print(f"{year} is not a leap year.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2020)
|
||||
main(2021)
|
||||
|
||||
|
||||
Output:
|
||||
2020 is a leap year.
|
||||
2021 is not a leap year.
|
||||
|
||||
|
||||
Question 09: Write a Python program to check if a number is positive, negative, or zero.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
if num > 0:
|
||||
print(f"{num} is positive.")
|
||||
elif num < 0:
|
||||
print(f"{num} is negative.")
|
||||
else:
|
||||
print("The number is zero.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10)
|
||||
main(-5)
|
||||
main(0)
|
||||
|
||||
|
||||
Output:
|
||||
10 is positive.
|
||||
-5 is negative.
|
||||
The number is zero.
|
||||
|
||||
|
||||
Question 10: Write a Python program to accept marks of a student and print the grade according to the given criteria:
|
||||
|
||||
Answer:
|
||||
- Marks ≥ 90: A
|
||||
- Marks ≥ 80: B
|
||||
- Marks ≥ 70: C
|
||||
- Marks ≥ 60: D
|
||||
- Otherwise: Fail
|
||||
"""
|
||||
|
||||
def main(marks: int) -> None:
|
||||
if marks >= 90:
|
||||
grade = 'A'
|
||||
elif marks >= 80:
|
||||
grade = 'B'
|
||||
elif marks >= 70:
|
||||
grade = 'C'
|
||||
elif marks >= 60:
|
||||
grade = 'D'
|
||||
else:
|
||||
grade = 'Fail'
|
||||
|
||||
print(f"Grade: {grade}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(95)
|
||||
main(85)
|
||||
main(75)
|
||||
main(65)
|
||||
main(55)
|
||||
|
||||
Output:
|
||||
Grade: A
|
||||
Grade: B
|
||||
Grade: C
|
||||
Grade: D
|
||||
Grade: Fail
|
||||
|
||||
|
||||
Question 11: Write a Python program to check whether a given character is a vowel or consonant.
|
||||
|
||||
Answer:
|
||||
def main(char: str) -> None:
|
||||
vowels = 'aeiou'
|
||||
if char.lower() in vowels:
|
||||
print(f"{char} is a vowel.")
|
||||
elif char.isalpha():
|
||||
print(f"{char} is a consonant.")
|
||||
else:
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('a')
|
||||
main('b')
|
||||
main('A')
|
||||
main('1') # also works with a int
|
||||
|
||||
|
||||
Output:
|
||||
Traceback (most recent call last):
|
||||
File "\\nas.local\share\data\school\Grade 11 CS\Practical File\question_11.py", line 17, in <module>
|
||||
main('1') # also works with a int
|
||||
^^^^^^^^^
|
||||
File "\\nas.local\share\data\school\Grade 11 CS\Practical File\question_11.py", line 10, in main
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
ValueError: Invalid input. Please enter an alphabetic character.
|
||||
|
||||
|
||||
Question 12: Write a Python program to check whether a number is divisible by both 5 and 11.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
if num % 5 == 0 and num % 11 == 0:
|
||||
print(f"{num} is divisible by both 5 and 11.")
|
||||
else:
|
||||
print(f"{num} is not divisible by both 5 and 11.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(55)
|
||||
main(22)
|
||||
main(10)
|
||||
main(121)
|
||||
|
||||
|
||||
Output:
|
||||
55 is divisible by both 5 and 11.
|
||||
22 is not divisible by both 5 and 11.
|
||||
10 is not divisible by both 5 and 11.
|
||||
121 is not divisible by both 5 and 11.
|
||||
|
||||
|
||||
Question 13: Write a Python program to find the absolute value of a number.
|
||||
|
||||
Answer:
|
||||
def main(num: float) -> None:
|
||||
abs_value = abs(num)
|
||||
print(f"The absolute value of {num} is {abs_value}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(-5.5)
|
||||
main(3.14)
|
||||
main(0)
|
||||
|
||||
|
||||
Output:
|
||||
The absolute value of -5.5 is 5.5
|
||||
The absolute value of 3.14 is 3.14
|
||||
The absolute value of 0 is 0
|
||||
|
||||
|
||||
Question 14: Write a Python program to check if a character is an uppercase or lowercase letter.
|
||||
|
||||
Answer:
|
||||
def main(char: str) -> None:
|
||||
if char.isupper():
|
||||
print(f"{char} is an uppercase letter.")
|
||||
elif char.islower():
|
||||
print(f"{char} is a lowercase letter.")
|
||||
else:
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('A')
|
||||
main('a')
|
||||
main('B')
|
||||
main('b')
|
||||
main('1') # also works with a int
|
||||
|
||||
Output:
|
||||
Traceback (most recent call last):
|
||||
File "\\nas.local\share\data\school\Grade 11 CS\Practical File\question_14.py", line 16, in <module>
|
||||
main('1') # also works with a int
|
||||
^^^^^^^^^
|
||||
File "\\nas.local\share\data\school\Grade 11 CS\Practical File\question_14.py", line 9, in main
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
ValueError: Invalid input. Please enter an alphabetic character.
|
||||
|
||||
|
||||
Question 15: Write a Python program to accept age and check whether the person is eligible to vote (18 or older).
|
||||
|
||||
Answer:
|
||||
def main(age: int) -> None:
|
||||
if age >= 18:
|
||||
print(f"Age {age} is eligible to vote.")
|
||||
else:
|
||||
print(f"Age {age} is not eligible to vote.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(18)
|
||||
main(17)
|
||||
main(20)
|
||||
|
||||
|
||||
Output:
|
||||
Age 18 is eligible to vote.
|
||||
Age 17 is not eligible to vote.
|
||||
Age 20 is eligible to vote.
|
||||
|
||||
|
||||
Question 16: Write a Python program to accept three sides and check whether they form a valid triangle.
|
||||
|
||||
Answer:
|
||||
def main(sides: tuple[int, int, int]) -> None:
|
||||
a, b, c = sides
|
||||
if a + b > c and a + c > b and b + c > a:
|
||||
print(f"The sides {sides} form a valid triangle.")
|
||||
else:
|
||||
print(f"The sides {sides} do not form a valid triangle.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main((3, 4, 5))
|
||||
main((1, 2, 3))
|
||||
main((5, 12, 13))
|
||||
main((7, 10, 5))
|
||||
main((1, 1, 2))
|
||||
|
||||
|
||||
Output:
|
||||
The sides (3, 4, 5) form a valid triangle.
|
||||
The sides (1, 2, 3) do not form a valid triangle.
|
||||
The sides (5, 12, 13) form a valid triangle.
|
||||
The sides (7, 10, 5) form a valid triangle.
|
||||
The sides (1, 1, 2) do not form a valid triangle.
|
||||
|
||||
|
||||
Question 17: Write a Python program to accept cost price and selling price, then print whether there is profit, loss, or no gain.
|
||||
|
||||
Answer:
|
||||
def main(cost_price: float, selling_price: float) -> None:
|
||||
if selling_price > cost_price:
|
||||
print(f"Profit: {selling_price - cost_price}")
|
||||
elif selling_price < cost_price:
|
||||
print(f"Loss: {cost_price - selling_price}")
|
||||
else:
|
||||
print("No gain, no loss.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(100.0, 120.0)
|
||||
main(150.0, 130.0)
|
||||
main(200.0, 200.0)
|
||||
|
||||
Output:
|
||||
Profit: 20.0
|
||||
Loss: 20.0
|
||||
No gain, no loss.
|
||||
|
||||
|
||||
Question 18: Write a Python program to accept two numbers and check whether they are equal or not
|
||||
|
||||
Answer:
|
||||
def main(num1: int, num2: int) -> None:
|
||||
if num1 == num2:
|
||||
print(f"{num1} and {num2} are equal.")
|
||||
else:
|
||||
print(f"{num1} and {num2} are not equal.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 10)
|
||||
main(10, 20)
|
||||
|
||||
Output:
|
||||
10 and 10 are equal.
|
||||
10 and 20 are not equal.
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
Question 01:
|
||||
Write a Python program to accept two integers and print their sum.
|
||||
|
||||
Answer:
|
||||
def main(int1: int, int2: int) -> int:
|
||||
print(f"{int1} + {int2} = {int1 + int2}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2, 2)
|
||||
|
||||
|
||||
Output:
|
||||
2 + 2 = 4
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
Question 02:
|
||||
Write a Python program that accepts the radius of a circle and prints its area.
|
||||
|
||||
Answer:
|
||||
def main(radius: int):
|
||||
print(f"Area: {3.14 * radius * radius}")
|
||||
# or you can do 3.13 * radius ** 2
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(7)
|
||||
|
||||
|
||||
Output:
|
||||
Area: 153.86
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
Question 03:
|
||||
Write a Python program to accept the length and width of a rectangle and compute its perimeter and area.
|
||||
|
||||
Answer:
|
||||
def main(length: int, width: int) -> None:
|
||||
print("Perimeter:", 2 * (length + width))
|
||||
print("Area:", length * width)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 10)
|
||||
|
||||
|
||||
Output:
|
||||
Perimeter: 40
|
||||
Area: 100
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
Question 04:
|
||||
Write a Python program to compute simple interest for a given principal amount, time, and rate of interest.
|
||||
|
||||
Answer:
|
||||
def main(principal: int, time: int, rate: float) -> None:
|
||||
print(f"Simple Interest: {principal * rate * time / 100}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10000, 10, 5.0)
|
||||
|
||||
|
||||
Output:
|
||||
Simple Interest: 5000.0
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
Question 05:
|
||||
Write a Python program to find whether a given number is even or odd.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
print("Even" if num % 2 == 0 else "Odd")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2)
|
||||
main(3)
|
||||
|
||||
|
||||
Output:
|
||||
Even
|
||||
Odd
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
Question 06:
|
||||
Write a Python program to find the largest among three numbers.
|
||||
|
||||
Answer:
|
||||
def main(numbers: list[int]) -> None:
|
||||
if not numbers:
|
||||
print("No numbers provided.")
|
||||
return
|
||||
|
||||
largest = numbers[0]
|
||||
for num in numbers:
|
||||
if num > largest:
|
||||
largest = num
|
||||
|
||||
print("Largest number:", largest)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main([1, 2, 3])
|
||||
main([3, 1, 2])
|
||||
main([5, 10, 15, 20])
|
||||
|
||||
|
||||
Output:
|
||||
Largest number: 3
|
||||
Largest number: 3
|
||||
Largest number: 20
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
Question 07:
|
||||
Write a Python program to perform arithmetic calculation. The program should accept two operands and an operator, then display the result.
|
||||
|
||||
Answer:
|
||||
def main(num1: float, num2: float, operator: str) -> None:
|
||||
if operator == '+':
|
||||
result = num1 + num2
|
||||
elif operator == '-':
|
||||
result = num1 - num2
|
||||
elif operator == '*':
|
||||
result = num1 * num2
|
||||
elif operator == '/':
|
||||
if num2 == 0:
|
||||
print("Error: Division by zero")
|
||||
raise ZeroDivisionError
|
||||
result = num1 / num2
|
||||
else:
|
||||
print("Error: Invalid operator")
|
||||
raise ValueError("Invalid operator. Use +, -, *, or /.")
|
||||
|
||||
print(f"Result: {result}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 5, '+')
|
||||
main(10, 5, '-')
|
||||
main(10, 5, '*')
|
||||
main(10, 5, '/')
|
||||
|
||||
|
||||
Output:
|
||||
Result: 15
|
||||
Result: 5
|
||||
Result: 50
|
||||
Result: 2.0
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
Question 08:
|
||||
Write a Python program to check whether a given year is a leap year or not.
|
||||
|
||||
Answer:
|
||||
def main(year: int) -> None:
|
||||
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
|
||||
print(f"{year} is a leap year.")
|
||||
else:
|
||||
print(f"{year} is not a leap year.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2020)
|
||||
main(2021)
|
||||
|
||||
|
||||
Output:
|
||||
2020 is a leap year.
|
||||
2021 is not a leap year.
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
Question 09:
|
||||
Write a Python program to check if a number is positive, negative, or zero.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
if num > 0:
|
||||
print(f"{num} is positive.")
|
||||
elif num < 0:
|
||||
print(f"{num} is negative.")
|
||||
else:
|
||||
print("The number is zero.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10)
|
||||
main(-5)
|
||||
main(0)
|
||||
|
||||
|
||||
Output:
|
||||
10 is positive.
|
||||
-5 is negative.
|
||||
The number is zero.
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
Question 10:
|
||||
Write a Python program to accept marks of a student and print the grade according to the given criteria:
|
||||
|
||||
Answer:
|
||||
- Marks ≥ 90: A
|
||||
- Marks ≥ 80: B
|
||||
- Marks ≥ 70: C
|
||||
- Marks ≥ 60: D
|
||||
- Otherwise: Fail
|
||||
"""
|
||||
|
||||
def main(marks: int) -> None:
|
||||
if marks >= 90:
|
||||
grade = 'A'
|
||||
elif marks >= 80:
|
||||
grade = 'B'
|
||||
elif marks >= 70:
|
||||
grade = 'C'
|
||||
elif marks >= 60:
|
||||
grade = 'D'
|
||||
else:
|
||||
grade = 'Fail'
|
||||
|
||||
print(f"Grade: {grade}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(95)
|
||||
main(85)
|
||||
main(75)
|
||||
main(65)
|
||||
main(55)
|
||||
|
||||
Output:
|
||||
Grade: A
|
||||
Grade: B
|
||||
Grade: C
|
||||
Grade: D
|
||||
Grade: Fail
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
Question 11:
|
||||
Write a Python program to check whether a given character is a vowel or consonant.
|
||||
|
||||
Answer:
|
||||
def main(char: str) -> None:
|
||||
vowels = 'aeiou'
|
||||
if char.lower() in vowels:
|
||||
print(f"{char} is a vowel.")
|
||||
elif char.isalpha():
|
||||
print(f"{char} is a consonant.")
|
||||
else:
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('a')
|
||||
main('b')
|
||||
main('A')
|
||||
main('1') # also works with a int
|
||||
|
||||
|
||||
Output:
|
||||
Traceback (most recent call last):
|
||||
File "\\homeassistant.local\share\data\school\Grade 11 CS\Practical File\question_11.py", line 17, in <module>
|
||||
main('1') # also works with a int
|
||||
^^^^^^^^^
|
||||
File "\\homeassistant.local\share\data\school\Grade 11 CS\Practical File\question_11.py", line 10, in main
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
ValueError: Invalid input. Please enter an alphabetic character.
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
Question 12:
|
||||
Write a Python program to check whether a number is divisible by both 5 and 11.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
if num % 5 == 0 and num % 11 == 0:
|
||||
print(f"{num} is divisible by both 5 and 11.")
|
||||
else:
|
||||
print(f"{num} is not divisible by both 5 and 11.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(55)
|
||||
main(22)
|
||||
main(10)
|
||||
main(121)
|
||||
|
||||
|
||||
Output:
|
||||
55 is divisible by both 5 and 11.
|
||||
22 is not divisible by both 5 and 11.
|
||||
10 is not divisible by both 5 and 11.
|
||||
121 is not divisible by both 5 and 11.
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
Question 13:
|
||||
Write a Python program to find the absolute value of a number.
|
||||
|
||||
Answer:
|
||||
def main(num: float) -> None:
|
||||
abs_value = abs(num)
|
||||
print(f"The absolute value of {num} is {abs_value}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(-5.5)
|
||||
main(3.14)
|
||||
main(0)
|
||||
|
||||
|
||||
Output:
|
||||
The absolute value of -5.5 is 5.5
|
||||
The absolute value of 3.14 is 3.14
|
||||
The absolute value of 0 is 0
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
Question 14:
|
||||
Write a Python program to check if a character is an uppercase or lowercase letter.
|
||||
|
||||
Answer:
|
||||
def main(char: str) -> None:
|
||||
if char.isupper():
|
||||
print(f"{char} is an uppercase letter.")
|
||||
elif char.islower():
|
||||
print(f"{char} is a lowercase letter.")
|
||||
else:
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('A')
|
||||
main('a')
|
||||
main('B')
|
||||
main('b')
|
||||
main('1') # also works with a int
|
||||
|
||||
Output:
|
||||
Traceback (most recent call last):
|
||||
File "\\homeassistant.local\share\data\school\Grade 11 CS\Practical File\question_14.py", line 16, in <module>
|
||||
main('1') # also works with a int
|
||||
^^^^^^^^^
|
||||
File "\\homeassistant.local\share\data\school\Grade 11 CS\Practical File\question_14.py", line 9, in main
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
ValueError: Invalid input. Please enter an alphabetic character.
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
Question 15:
|
||||
Write a Python program to accept age and check whether the person is eligible to vote (18 or older).
|
||||
|
||||
Answer:
|
||||
def main(age: int) -> None:
|
||||
if age >= 18:
|
||||
print(f"Age {age} is eligible to vote.")
|
||||
else:
|
||||
print(f"Age {age} is not eligible to vote.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(18)
|
||||
main(17)
|
||||
main(20)
|
||||
|
||||
|
||||
Output:
|
||||
Age 18 is eligible to vote.
|
||||
Age 17 is not eligible to vote.
|
||||
Age 20 is eligible to vote.
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
Question 16:
|
||||
Write a Python program to accept three sides and check whether they form a valid triangle.
|
||||
|
||||
Answer:
|
||||
def main(sides: tuple[int, int, int]) -> None:
|
||||
a, b, c = sides
|
||||
if a + b > c and a + c > b and b + c > a:
|
||||
print(f"The sides {sides} form a valid triangle.")
|
||||
else:
|
||||
print(f"The sides {sides} do not form a valid triangle.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main((3, 4, 5))
|
||||
main((1, 2, 3))
|
||||
main((5, 12, 13))
|
||||
main((7, 10, 5))
|
||||
main((1, 1, 2))
|
||||
|
||||
|
||||
Output:
|
||||
The sides (3, 4, 5) form a valid triangle.
|
||||
The sides (1, 2, 3) do not form a valid triangle.
|
||||
The sides (5, 12, 13) form a valid triangle.
|
||||
The sides (7, 10, 5) form a valid triangle.
|
||||
The sides (1, 1, 2) do not form a valid triangle.
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
Question 17:
|
||||
Write a Python program to accept cost price and selling price, then print whether there is profit, loss, or no gain.
|
||||
|
||||
Answer:
|
||||
def main(cost_price: float, selling_price: float) -> None:
|
||||
if selling_price > cost_price:
|
||||
print(f"Profit: {selling_price - cost_price}")
|
||||
elif selling_price < cost_price:
|
||||
print(f"Loss: {cost_price - selling_price}")
|
||||
else:
|
||||
print("No gain, no loss.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(100.0, 120.0)
|
||||
main(150.0, 130.0)
|
||||
main(200.0, 200.0)
|
||||
|
||||
Output:
|
||||
Profit: 20.0
|
||||
Loss: 20.0
|
||||
No gain, no loss.
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
Question 18:
|
||||
Write a Python program to accept two numbers and check whether they are equal or not
|
||||
|
||||
Answer:
|
||||
def main(num1: int, num2: int) -> None:
|
||||
if num1 == num2:
|
||||
print(f"{num1} and {num2} are equal.")
|
||||
else:
|
||||
print(f"{num1} and {num2} are not equal.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 10)
|
||||
main(10, 20)
|
||||
|
||||
Output:
|
||||
10 and 10 are equal.
|
||||
10 and 20 are not equal.
|
||||
|
|
@ -27,30 +27,9 @@ def create_question_file(question_text, question_num):
|
|||
with open(filename, 'w') as f:
|
||||
f.write(f"# Question: {question_text}\n\n")
|
||||
f.write("")
|
||||
f.write("def main():\n")
|
||||
f.write(" pass\n\n")
|
||||
f.write("")
|
||||
f.write("if __name__ == '__main__':\n")
|
||||
f.write(" main()\n")
|
||||
|
||||
return filename
|
||||
|
||||
def open_in_vscode(filename):
|
||||
try:
|
||||
vscode_commands = ['code', 'code-insiders', 'codium']
|
||||
|
||||
for cmd in vscode_commands:
|
||||
try:
|
||||
subprocess.run([cmd, filename], check=True)
|
||||
return True
|
||||
except (subprocess.CalledProcessError, FileNotFoundError):
|
||||
continue
|
||||
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
def wait_for_completion():
|
||||
input("Press Enter when the question is done: ")
|
||||
return True
|
||||
|
|
@ -104,7 +83,6 @@ def append_to_master_file(question_text, question_num, code_content, execution_o
|
|||
f.write(f"Question {question_num:02d}: {question_text}\n\n")
|
||||
f.write("Answer:\n")
|
||||
f.write(code_content)
|
||||
f.write("\n\nOutput:\n")
|
||||
|
||||
if execution_output['returncode'] == 0:
|
||||
if execution_output['stdout']:
|
||||
|
|
@ -149,7 +127,6 @@ def main():
|
|||
question_num = find_next_question_number()
|
||||
filename = create_question_file(question, question_num)
|
||||
|
||||
open_in_vscode(filename)
|
||||
wait_for_completion()
|
||||
|
||||
code_content = read_code_from_file(filename)
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
# Question: Write a Python program to accept two integers and print their sum.
|
||||
|
||||
def main(int1: int, int2: int) -> int:
|
||||
print(f"{int1} + {int2} = {int1 + int2}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2, 2)
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
Question 01:
|
||||
Write a Python program to accept two integers and print their sum.
|
||||
|
||||
Answer:
|
||||
def main(int1: int, int2: int) -> int:
|
||||
print(f"{int1} + {int2} = {int1 + int2}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2, 2)
|
||||
|
||||
|
||||
Output:
|
||||
2 + 2 = 4
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
# Question: Write a Python program that accepts the radius of a circle and prints its area.
|
||||
|
||||
def main(radius: int):
|
||||
print(f"Area: {3.14 * radius * radius}")
|
||||
# or you can do 3.13 * radius ** 2
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(7)
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
Question 02:
|
||||
Write a Python program that accepts the radius of a circle and prints its area.
|
||||
|
||||
Answer:
|
||||
def main(radius: int):
|
||||
print(f"Area: {3.14 * radius * radius}")
|
||||
# or you can do 3.13 * radius ** 2
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(7)
|
||||
|
||||
|
||||
Output:
|
||||
Area: 153.86
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
# Question: Write a Python program to accept the length and width of a rectangle and compute its perimeter and area.
|
||||
|
||||
def main(length: int, width: int) -> None:
|
||||
print("Perimeter:", 2 * (length + width))
|
||||
print("Area:", length * width)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 10)
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
Question 03:
|
||||
Write a Python program to accept the length and width of a rectangle and compute its perimeter and area.
|
||||
|
||||
Answer:
|
||||
def main(length: int, width: int) -> None:
|
||||
print("Perimeter:", 2 * (length + width))
|
||||
print("Area:", length * width)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 10)
|
||||
|
||||
|
||||
Output:
|
||||
Perimeter: 40
|
||||
Area: 100
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
# Question: Write a Python program to compute simple interest for a given principal amount, time, and rate of interest.
|
||||
|
||||
def main(principal: int, time: int, rate: float) -> None:
|
||||
print(f"Simple Interest: {principal * rate * time / 100}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10000, 10, 5.0)
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
Question 04:
|
||||
Write a Python program to compute simple interest for a given principal amount, time, and rate of interest.
|
||||
|
||||
Answer:
|
||||
def main(principal: int, time: int, rate: float) -> None:
|
||||
print(f"Simple Interest: {principal * rate * time / 100}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10000, 10, 5.0)
|
||||
|
||||
|
||||
Output:
|
||||
Simple Interest: 5000.0
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
# Question: Write a Python program to find whether a given number is even or odd.
|
||||
|
||||
def main(num: int) -> None:
|
||||
print("Even" if num % 2 == 0 else "Odd")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2)
|
||||
main(3)
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
Question 05:
|
||||
Write a Python program to find whether a given number is even or odd.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
print("Even" if num % 2 == 0 else "Odd")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2)
|
||||
main(3)
|
||||
|
||||
|
||||
Output:
|
||||
Even
|
||||
Odd
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
# Question: Write a Python program to find the largest among three numbers.
|
||||
|
||||
def main(numbers: list[int]) -> None:
|
||||
if not numbers:
|
||||
print("No numbers provided.")
|
||||
return
|
||||
|
||||
largest = numbers[0]
|
||||
for num in numbers:
|
||||
if num > largest:
|
||||
largest = num
|
||||
|
||||
print("Largest number:", largest)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main([1, 2, 3])
|
||||
main([3, 1, 2])
|
||||
main([5, 10, 15, 20])
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
Question 06:
|
||||
Write a Python program to find the largest among three numbers.
|
||||
|
||||
Answer:
|
||||
def main(numbers: list[int]) -> None:
|
||||
if not numbers:
|
||||
print("No numbers provided.")
|
||||
return
|
||||
|
||||
largest = numbers[0]
|
||||
for num in numbers:
|
||||
if num > largest:
|
||||
largest = num
|
||||
|
||||
print("Largest number:", largest)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main([1, 2, 3])
|
||||
main([3, 1, 2])
|
||||
main([5, 10, 15, 20])
|
||||
|
||||
|
||||
Output:
|
||||
Largest number: 3
|
||||
Largest number: 3
|
||||
Largest number: 20
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
# Question: Write a Python program to perform arithmetic calculation. The program should accept two operands and an operator, then display the result.
|
||||
|
||||
def main(num1: float, num2: float, operator: str) -> None:
|
||||
if operator == '+':
|
||||
result = num1 + num2
|
||||
elif operator == '-':
|
||||
result = num1 - num2
|
||||
elif operator == '*':
|
||||
result = num1 * num2
|
||||
elif operator == '/':
|
||||
if num2 == 0:
|
||||
print("Error: Division by zero")
|
||||
raise ZeroDivisionError
|
||||
result = num1 / num2
|
||||
else:
|
||||
print("Error: Invalid operator")
|
||||
raise ValueError("Invalid operator. Use +, -, *, or /.")
|
||||
|
||||
print(f"Result: {result}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 5, '+')
|
||||
main(10, 5, '-')
|
||||
main(10, 5, '*')
|
||||
main(10, 5, '/')
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
Question 07:
|
||||
Write a Python program to perform arithmetic calculation. The program should accept two operands and an operator, then display the result.
|
||||
|
||||
Answer:
|
||||
def main(num1: float, num2: float, operator: str) -> None:
|
||||
if operator == '+':
|
||||
result = num1 + num2
|
||||
elif operator == '-':
|
||||
result = num1 - num2
|
||||
elif operator == '*':
|
||||
result = num1 * num2
|
||||
elif operator == '/':
|
||||
if num2 == 0:
|
||||
print("Error: Division by zero")
|
||||
raise ZeroDivisionError
|
||||
result = num1 / num2
|
||||
else:
|
||||
print("Error: Invalid operator")
|
||||
raise ValueError("Invalid operator. Use +, -, *, or /.")
|
||||
|
||||
print(f"Result: {result}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 5, '+')
|
||||
main(10, 5, '-')
|
||||
main(10, 5, '*')
|
||||
main(10, 5, '/')
|
||||
|
||||
|
||||
Output:
|
||||
Result: 15
|
||||
Result: 5
|
||||
Result: 50
|
||||
Result: 2.0
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
# Question: Write a Python program to check whether a given year is a leap year or not.
|
||||
|
||||
def main(year: int) -> None:
|
||||
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
|
||||
print(f"{year} is a leap year.")
|
||||
else:
|
||||
print(f"{year} is not a leap year.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2020)
|
||||
main(2021)
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
Question 08:
|
||||
Write a Python program to check whether a given year is a leap year or not.
|
||||
|
||||
Answer:
|
||||
def main(year: int) -> None:
|
||||
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
|
||||
print(f"{year} is a leap year.")
|
||||
else:
|
||||
print(f"{year} is not a leap year.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(2020)
|
||||
main(2021)
|
||||
|
||||
|
||||
Output:
|
||||
2020 is a leap year.
|
||||
2021 is not a leap year.
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
# Question: Write a Python program to check if a number is positive, negative, or zero.
|
||||
|
||||
def main(num: int) -> None:
|
||||
if num > 0:
|
||||
print(f"{num} is positive.")
|
||||
elif num < 0:
|
||||
print(f"{num} is negative.")
|
||||
else:
|
||||
print("The number is zero.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10)
|
||||
main(-5)
|
||||
main(0)
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
Question 09:
|
||||
Write a Python program to check if a number is positive, negative, or zero.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
if num > 0:
|
||||
print(f"{num} is positive.")
|
||||
elif num < 0:
|
||||
print(f"{num} is negative.")
|
||||
else:
|
||||
print("The number is zero.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10)
|
||||
main(-5)
|
||||
main(0)
|
||||
|
||||
|
||||
Output:
|
||||
10 is positive.
|
||||
-5 is negative.
|
||||
The number is zero.
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
"""
|
||||
Question: Write a Python program to accept marks of a student and print the grade according to the given criteria:
|
||||
- Marks ≥ 90: A
|
||||
- Marks ≥ 80: B
|
||||
- Marks ≥ 70: C
|
||||
- Marks ≥ 60: D
|
||||
- Otherwise: Fail
|
||||
"""
|
||||
|
||||
def main(marks: int) -> None:
|
||||
if marks >= 90:
|
||||
grade = 'A'
|
||||
elif marks >= 80:
|
||||
grade = 'B'
|
||||
elif marks >= 70:
|
||||
grade = 'C'
|
||||
elif marks >= 60:
|
||||
grade = 'D'
|
||||
else:
|
||||
grade = 'Fail'
|
||||
|
||||
print(f"Grade: {grade}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(95)
|
||||
main(85)
|
||||
main(75)
|
||||
main(65)
|
||||
main(55)
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
Question 10:
|
||||
Write a Python program to accept marks of a student and print the grade according to the given criteria:
|
||||
|
||||
Answer:
|
||||
- Marks ≥ 90: A
|
||||
- Marks ≥ 80: B
|
||||
- Marks ≥ 70: C
|
||||
- Marks ≥ 60: D
|
||||
- Otherwise: Fail
|
||||
"""
|
||||
|
||||
def main(marks: int) -> None:
|
||||
if marks >= 90:
|
||||
grade = 'A'
|
||||
elif marks >= 80:
|
||||
grade = 'B'
|
||||
elif marks >= 70:
|
||||
grade = 'C'
|
||||
elif marks >= 60:
|
||||
grade = 'D'
|
||||
else:
|
||||
grade = 'Fail'
|
||||
|
||||
print(f"Grade: {grade}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(95)
|
||||
main(85)
|
||||
main(75)
|
||||
main(65)
|
||||
main(55)
|
||||
|
||||
Output:
|
||||
Grade: A
|
||||
Grade: B
|
||||
Grade: C
|
||||
Grade: D
|
||||
Grade: Fail
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
# Question: Write a Python program to check whether a given character is a vowel or consonant.
|
||||
|
||||
def main(char: str) -> None:
|
||||
vowels = 'aeiou'
|
||||
if char.lower() in vowels:
|
||||
print(f"{char} is a vowel.")
|
||||
elif char.isalpha():
|
||||
print(f"{char} is a consonant.")
|
||||
else:
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('a')
|
||||
main('b')
|
||||
main('A')
|
||||
main('1') # also works with a int
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
Question 11:
|
||||
Write a Python program to check whether a given character is a vowel or consonant.
|
||||
|
||||
Answer:
|
||||
def main(char: str) -> None:
|
||||
vowels = 'aeiou'
|
||||
if char.lower() in vowels:
|
||||
print(f"{char} is a vowel.")
|
||||
elif char.isalpha():
|
||||
print(f"{char} is a consonant.")
|
||||
else:
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('a')
|
||||
main('b')
|
||||
main('A')
|
||||
main('1') # also works with a int
|
||||
|
||||
|
||||
Output:
|
||||
Traceback (most recent call last):
|
||||
File "\\homeassistant.local\share\data\school\Grade 11 CS\Practical File\question_11.py", line 17, in <module>
|
||||
main('1') # also works with a int
|
||||
^^^^^^^^^
|
||||
File "\\homeassistant.local\share\data\school\Grade 11 CS\Practical File\question_11.py", line 10, in main
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
ValueError: Invalid input. Please enter an alphabetic character.
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
# Question: Write a Python program to check whether a number is divisible by both 5 and 11.
|
||||
|
||||
def main(num: int) -> None:
|
||||
if num % 5 == 0 and num % 11 == 0:
|
||||
print(f"{num} is divisible by both 5 and 11.")
|
||||
else:
|
||||
print(f"{num} is not divisible by both 5 and 11.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(55)
|
||||
main(22)
|
||||
main(10)
|
||||
main(121)
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
Question 12:
|
||||
Write a Python program to check whether a number is divisible by both 5 and 11.
|
||||
|
||||
Answer:
|
||||
def main(num: int) -> None:
|
||||
if num % 5 == 0 and num % 11 == 0:
|
||||
print(f"{num} is divisible by both 5 and 11.")
|
||||
else:
|
||||
print(f"{num} is not divisible by both 5 and 11.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(55)
|
||||
main(22)
|
||||
main(10)
|
||||
main(121)
|
||||
|
||||
|
||||
Output:
|
||||
55 is divisible by both 5 and 11.
|
||||
22 is not divisible by both 5 and 11.
|
||||
10 is not divisible by both 5 and 11.
|
||||
121 is not divisible by both 5 and 11.
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
# Question: Write a Python program to find the absolute value of a number.
|
||||
|
||||
def main(num: float) -> None:
|
||||
abs_value = abs(num)
|
||||
print(f"The absolute value of {num} is {abs_value}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(-5.5)
|
||||
main(3.14)
|
||||
main(0)
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
Question 13:
|
||||
Write a Python program to find the absolute value of a number.
|
||||
|
||||
Answer:
|
||||
def main(num: float) -> None:
|
||||
abs_value = abs(num)
|
||||
print(f"The absolute value of {num} is {abs_value}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(-5.5)
|
||||
main(3.14)
|
||||
main(0)
|
||||
|
||||
|
||||
Output:
|
||||
The absolute value of -5.5 is 5.5
|
||||
The absolute value of 3.14 is 3.14
|
||||
The absolute value of 0 is 0
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
# Question: Write a Python program to check if a character is an uppercase or lowercase letter.
|
||||
|
||||
def main(char: str) -> None:
|
||||
if char.isupper():
|
||||
print(f"{char} is an uppercase letter.")
|
||||
elif char.islower():
|
||||
print(f"{char} is a lowercase letter.")
|
||||
else:
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('A')
|
||||
main('a')
|
||||
main('B')
|
||||
main('b')
|
||||
main('1') # also works with a int
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
Question 14:
|
||||
Write a Python program to check if a character is an uppercase or lowercase letter.
|
||||
|
||||
Answer:
|
||||
def main(char: str) -> None:
|
||||
if char.isupper():
|
||||
print(f"{char} is an uppercase letter.")
|
||||
elif char.islower():
|
||||
print(f"{char} is a lowercase letter.")
|
||||
else:
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('A')
|
||||
main('a')
|
||||
main('B')
|
||||
main('b')
|
||||
main('1') # also works with a int
|
||||
|
||||
Output:
|
||||
Traceback (most recent call last):
|
||||
File "\\homeassistant.local\share\data\school\Grade 11 CS\Practical File\question_14.py", line 16, in <module>
|
||||
main('1') # also works with a int
|
||||
^^^^^^^^^
|
||||
File "\\homeassistant.local\share\data\school\Grade 11 CS\Practical File\question_14.py", line 9, in main
|
||||
raise ValueError("Invalid input. Please enter an alphabetic character.")
|
||||
ValueError: Invalid input. Please enter an alphabetic character.
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
# Question: Write a Python program to accept age and check whether the person is eligible to vote (18 or older).
|
||||
|
||||
def main(age: int) -> None:
|
||||
if age >= 18:
|
||||
print(f"Age {age} is eligible to vote.")
|
||||
else:
|
||||
print(f"Age {age} is not eligible to vote.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(18)
|
||||
main(17)
|
||||
main(20)
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
Question 15:
|
||||
Write a Python program to accept age and check whether the person is eligible to vote (18 or older).
|
||||
|
||||
Answer:
|
||||
def main(age: int) -> None:
|
||||
if age >= 18:
|
||||
print(f"Age {age} is eligible to vote.")
|
||||
else:
|
||||
print(f"Age {age} is not eligible to vote.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(18)
|
||||
main(17)
|
||||
main(20)
|
||||
|
||||
|
||||
Output:
|
||||
Age 18 is eligible to vote.
|
||||
Age 17 is not eligible to vote.
|
||||
Age 20 is eligible to vote.
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
# Question: Write a Python program to accept three sides and check whether they form a valid triangle.
|
||||
|
||||
def main(sides: tuple[int, int, int]) -> None:
|
||||
a, b, c = sides
|
||||
if a + b > c and a + c > b and b + c > a:
|
||||
print(f"The sides {sides} form a valid triangle.")
|
||||
else:
|
||||
print(f"The sides {sides} do not form a valid triangle.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main((3, 4, 5))
|
||||
main((1, 2, 3))
|
||||
main((5, 12, 13))
|
||||
main((7, 10, 5))
|
||||
main((1, 1, 2))
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
Question 16:
|
||||
Write a Python program to accept three sides and check whether they form a valid triangle.
|
||||
|
||||
Answer:
|
||||
def main(sides: tuple[int, int, int]) -> None:
|
||||
a, b, c = sides
|
||||
if a + b > c and a + c > b and b + c > a:
|
||||
print(f"The sides {sides} form a valid triangle.")
|
||||
else:
|
||||
print(f"The sides {sides} do not form a valid triangle.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main((3, 4, 5))
|
||||
main((1, 2, 3))
|
||||
main((5, 12, 13))
|
||||
main((7, 10, 5))
|
||||
main((1, 1, 2))
|
||||
|
||||
|
||||
Output:
|
||||
The sides (3, 4, 5) form a valid triangle.
|
||||
The sides (1, 2, 3) do not form a valid triangle.
|
||||
The sides (5, 12, 13) form a valid triangle.
|
||||
The sides (7, 10, 5) form a valid triangle.
|
||||
The sides (1, 1, 2) do not form a valid triangle.
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
# Question: Write a Python program to accept cost price and selling price, then print whether there is profit, loss, or no gain.
|
||||
|
||||
def main(cost_price: float, selling_price: float) -> None:
|
||||
if selling_price > cost_price:
|
||||
print(f"Profit: {selling_price - cost_price}")
|
||||
elif selling_price < cost_price:
|
||||
print(f"Loss: {cost_price - selling_price}")
|
||||
else:
|
||||
print("No gain, no loss.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(100.0, 120.0)
|
||||
main(150.0, 130.0)
|
||||
main(200.0, 200.0)
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
Question 17:
|
||||
Write a Python program to accept cost price and selling price, then print whether there is profit, loss, or no gain.
|
||||
|
||||
Answer:
|
||||
def main(cost_price: float, selling_price: float) -> None:
|
||||
if selling_price > cost_price:
|
||||
print(f"Profit: {selling_price - cost_price}")
|
||||
elif selling_price < cost_price:
|
||||
print(f"Loss: {cost_price - selling_price}")
|
||||
else:
|
||||
print("No gain, no loss.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(100.0, 120.0)
|
||||
main(150.0, 130.0)
|
||||
main(200.0, 200.0)
|
||||
|
||||
Output:
|
||||
Profit: 20.0
|
||||
Loss: 20.0
|
||||
No gain, no loss.
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
# Question: Write a Python program to accept two numbers and check whether they are equal or not
|
||||
|
||||
def main(num1: int, num2: int) -> None:
|
||||
if num1 == num2:
|
||||
print(f"{num1} and {num2} are equal.")
|
||||
else:
|
||||
print(f"{num1} and {num2} are not equal.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 10)
|
||||
main(10, 20)
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
Question 18:
|
||||
Write a Python program to accept two numbers and check whether they are equal or not
|
||||
|
||||
Answer:
|
||||
def main(num1: int, num2: int) -> None:
|
||||
if num1 == num2:
|
||||
print(f"{num1} and {num2} are equal.")
|
||||
else:
|
||||
print(f"{num1} and {num2} are not equal.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(10, 10)
|
||||
main(10, 20)
|
||||
|
||||
Output:
|
||||
10 and 10 are equal.
|
||||
10 and 20 are not equal.
|
||||
Loading…
Reference in a new issue