-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek 11.py
More file actions
176 lines (153 loc) · 5.04 KB
/
Copy pathweek 11.py
File metadata and controls
176 lines (153 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
1.
Write a Python program that asks the user for their age and prints a message based on the age. Ensure that the program handles cases where the input is not a valid integer.
Input Format: A single line input representing the user's age.
Output Format: Print a message based on the age or an error if the input is invalid.
For example:
Input Result
twenty
Error: Please enter a valid age.
25
You are 25 years old.
-1
Error: Please enter a valid age.
try:
a=input()
if(len(a)==0):
print("Error: Please enter a valid age.")
elif a.isnumeric():
print("You are",a,"years old.")
else:
print("Error: Please enter a valid age.")
except:
print("Error: Please enter a valid age.")
OUTOUT:
Input Expected Got
twenty Error: Please enter a valid age. Error: Please enter a valid age.
25 You are 25 years old. You are 25 years old.
-1 Error: Please enter a valid age. Error: Please enter a valid age.
150 You are 150 years old. You are 150 years old.
Error: Please enter a valid age. Error: Please enter a valid age.
Passed all tests!
Correct
2.
Problem Description:
Write a Python program that asks the user for their age and prints a message based on the age. Ensure that the program handles cases where the input is not a valid integer.
Input Format:
A single line input representing the user's age.
Output Format:
Print a message based on the age or an error if the input is invalid.
For example:
Input Result
25 You are 25 years old.
rec Error: Please enter a valid age.
-5 Error: Please enter a valid age.
try:
a=input()
if(len(a)==0):
print("Error: Please enter a valid age.")
elif a.isnumeric():
print("You are",a,"years old.")
else:
print("Error: Please enter a valid age.")
except:
print("Error: Please enter a valid age.")
OUTPUT:
Input Expected Got
25 You are 25 years old. You are 25 years old.
rec Error: Please enter a valid age. Error: Please enter a valid age.
!@# Error: Please enter a valid age. Error: Please enter a valid age.
Passed all tests!
Correct
3.
Problem Description:
Write a Python script that asks the user to enter a number within a specified range (e.g., 1 to 100). Handle exceptions for invalid inputs and out-of-range numbers.
Input Format:
User inputs a number.
Output Format:
Confirm the input or print an error message if it's invalid or out of range.
For example:
Input Result
1 Valid input.
101 Error: Number out of allowed range
rec Error: invalid literal for int()
def main():
min_range = 1
max_range = 100
try:
num = int(input())
if num < min_range or num > max_range:
print("Error: Number out of allowed range")
else:
print("Valid input.")
except ValueError:
print("Error: invalid literal for int()")
if __name__ == "__main__":
OUTPUT:
Input Expected Got
1 Valid input. Valid input.
100 Valid input. Valid input.
101 Error: Number out of allowed range Error: Number out of allowed range
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
4.
Develop a Python program that safely performs division between two numbers provided by the user. Handle exceptions like division by zero and non-numeric inputs.
Input Format: Two lines of input, each containing a number.
Output Format: Print the result of the division or an error message if an exception occurs.
For example:
Input Result
10
2 5.0
10
0 Error: Cannot divide or modulo by zero.
ten
5
Error: Non-numeric input provided.
def main():
try:
num1 = float(input())
num2 = float(input())
division_result = num1 / num2
modulo_result = num1 % num2
print(division_result)
except ValueError:
print("Error: Non-numeric input provided.")
except ZeroDivisionError:
print("Error: Cannot divide or modulo by zero.")
if __name__ == "__main__":
main()
OUTPUT:
Input Expected Got
10
2 5.0 5.0
10
0 Error: Cannot divide or modulo by zero. Error: Cannot divide or modulo by zero.
ten
5 Error: Non-numeric input provided. Error: Non-numeric input provided.
Passed all tests!
Correct
5.
Problem Description:
Develop a Python program that safely calculates the square root of a number provided by the user. Handle exceptions for negative inputs and non-numeric inputs.
Input Format:
User inputs a number.
Output Format:
Print the square root of the number or an error message if an exception occurs.
For example:
Input Result
16 The square root of 16.0 is 4.00
-4 Error: Cannot calculate the square root of a negative number.
rec Error: could not convert string to float
try:
a=float(input())
if(a<0):
print("Error: Cannot calculate the square root of a negative number.")
else:
print("The square root of",a,"is {:.2f}".format(a**0.5))
except:
print("Error: could not convert string to float")
OUTPUT:
Input Expected Got
16 The square root of 16.0 is 4.00 The square root of 16.0 is 4.00
0 The square root of 0.0 is 0.00 The square root of 0.0 is 0.00
-4 Error: Cannot calculate the square root of a negative number.