1x = 5
2if x > 0
3 print(x)
1for i in range(5):
2print(i)
1length = 5
2width = 4
3area_rectangle = length + width
4print(area_rectangle)
1length = 5
2width = 4
3area_rectangle = length * width
4print(area_rectangle)
1a = b = c = 10
2average = a + b + c / 3
3print(average)
1a = b = c = 10
2average = (a + b + c) / 3
3print(average)
1score = 45
2if score < 50:
3 print("Excellent")
1score = 45
2if score < 50:
3 print("Fail")
1try:
2 # Code that might cause an error
3except ErrorType:
4 # Code to execute if an error occurs
5else:
6 # Code to execute if no error occurs
7finally:
8 # Code that always executes
1try:
2 print("School Trip Cost Sharing Calculator")
3 cost_total = float(input("Enter total trip cost (KWD): "))
4 students = int(input("Number of participating students: "))
5 share = cost_total / students
6except ValueError:
7 print("Input Error: Please enter valid numbers only!")
8except ZeroDivisionError:
9 print("Error: Cannot divide by zero - please check student count")
10else:
11 print(f"Each student's share: {share:.2f} KWD")
12finally:
13 print("Program ended!")
1try:
2 print("School Trip Cost Sharing Calculator")
3 cost_total = float(input("Enter total trip cost (KWD): "))
4 students = int(input("Number of participating students: "))
5 share = cost_total / students
6except ValueError:
7 print("Input Error: Please enter valid numbers only!")
8except ZeroDivisionError:
9 print("Error: Cannot divide by zero - please check student count")
10else:
11 print(f"Each student's share: {share:.2f} KWD")
12finally:
13 print("Program ended!")
1try:
2 print("School Trip Cost Sharing Calculator")
3 cost_total = float(input("Enter total trip cost (KWD): "))
4 students = int(input("Number of participating students: "))
5 share = cost_total / students
6except ValueError:
7 print("Input Error: Please enter valid numbers only!")
8except ZeroDivisionError:
9 print("Error: Cannot divide by zero - please check student count")
10else:
11 print(f"Each student's share: {share:.2f} KWD")
12finally:
13 print("Program ended!")
1try:
2 print("School Trip Cost Sharing Calculator")
3 cost_total = float(input("Enter total trip cost (KWD): "))
4 students = int(input("Number of participating students: "))
5 share = cost_total / students
6except ValueError:
7 print("Input Error: Please enter valid numbers only!")
8except ZeroDivisionError:
9 print("Error: Cannot divide by zero - please check student count")
10else:
11 print(f"Each student's share: {share:.2f} KWD")
12finally:
13 print("Program ended!")
1try:
2 print("School Trip Cost Sharing Calculator")
3 cost_total = float(input("Enter total trip cost (KWD): "))
4 students = int(input("Number of participating students: "))
5 share = cost_total / students
6 print(f"Each student's share: {share:.2f} KWD")
7except Exception as e:
8 print('Error is:', e)
9finally:
10 print("Program ended!")
1while True:
2 try:
3 number = int(input('Enter a number:'))
4 if number % 2 == 0:
5 print('The number is even.')
6 else:
7 print('The number is odd.')
8 except ValueError:
9 print('You did not enter a valid number.')
1try:
2 n = int(input("Enter a number: "))
3 print(100 / n)
4except ValueError:
5 print("Not a number")
6except ZeroDivisionError:
7 print("Cannot divide by zero")
8finally:
9 print("Done")
▶ Run
File "main.py", line 2
SyntaxError: unmatched ')'
Process finished with exit code 1
5
File "main.py", line 2
SyntaxError: expected ':'
Process finished with exit code 1
File "main.py", line 1
SyntaxError: '(' was never closed
Process finished with exit code 1
File "main.py", line 2
IndentationError: expected an indented block after 'for' statement on line 1
Process finished with exit code 1
Traceback (most recent call last):
File "main.py", line 1, in <module>
ZeroDivisionError: division by zero
Process finished with exit code 1
Traceback (most recent call last):
File "main.py", line 2, in <module>
NameError: name 'z' is not defined
Process finished with exit code 1
Traceback (most recent call last):
File "main.py", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
Process finished with exit code 1
Traceback (most recent call last):
File "main.py", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'
Process finished with exit code 1
9
20
23.333333333333332
10.0
Excellent
Fail
School Trip Cost Sharing Calculator
Enter total trip cost (KWD): 12
Number of participating students: 4
Each student's share: 3.00 KWD
Program ended!
School Trip Cost Sharing Calculator
Enter total trip cost (KWD): abc
Input Error: Please enter valid numbers only!
Program ended!
School Trip Cost Sharing Calculator
Enter total trip cost (KWD): 1.5
Number of participating students: 0
Error: Cannot divide by zero - please check student count
Program ended!
School Trip Cost Sharing Calculator
Enter total trip cost (KWD): 10
Number of participating students: 0
Error is: float division by zero
Program ended!
Enter a number:56
The number is even.
Enter a number:8.9
You did not enter a valid number.
Enter a number:11GR
You did not enter a valid number.
Enter a number:
Enter a number: 0
Cannot divide by zero
Done