-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek_11.txt
86 lines (85 loc) · 2.66 KB
/
week_11.txt
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
resource url?
QUESTION ----------------
Problem Description:
------------------ANSWER
import math as m
try:
n = float(input())
if n < 0:
print("Error: Cannot calculate the square root of a negative number.")
else:
sqrt = m.sqrt(n)
print(f"The square root of {n} is {sqrt:.2f}")
except ValueError:
print("Error: could not convert string to float")
------------------------------
QUESTION ----------------
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.
------------------ANSWER
while True:
try:
n=int(input())
if n>=0:
print("You are ",n," years old.",sep='')
break
else:
print("Error: Please enter a valid age.")
break
except:
print("Error: Please enter a valid age.")
break
------------------------------
QUESTION ----------------
Problem Description:
------------------ANSWER
while True:
try:
n=int(input())
if n>=1 and n<=100:
print("Valid input.")
break
else:
print("Error: Number out of allowed range")
break
except:
print("Error: invalid literal for int()")
break
------------------------------
QUESTION ----------------
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.
------------------ANSWER
try:
num1 = float(input())
num2 = float(input())
if num2 == 0:
print("Error: Cannot divide or modulo by zero.")
else:
result = num1 / num2
print(result)
except ValueError:
print("Error: Non-numeric input provided.")
------------------------------
QUESTION ----------------
Write a Python program that performs division and modulo operations on two numbers provided by the user. Handle division by zero and non-numeric inputs.
------------------ANSWER
while True:
try:
a=int(input())
b=int(input())
if b==0:
print("Error: Cannot divide or modulo by zero.")
break
else:
m=a%b
if m==0:
print("Division result: %.1f"%(a/b))
print("Modulo result: %d"%m)
break
else:
print("Division result: %.16f"%(a/b))
print("Modulo result: %d"%(a%b))
break
except:
print("Error: Non-numeric input provided.")
break
------------------------------