-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandling_errors.py
106 lines (70 loc) · 2.54 KB
/
handling_errors.py
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
#!/usr/bin/env python
# coding: utf-8
# # Try statement
# In[1]:
# Prompt the user to input a number
try:
x = int(input("Enter your Number: "))
except:
# If the input is not a valid integer, print an error message
print("That's not a valid Number !")
# # Try statement with while loop
# In[3]:
# Continuously prompt the user to input a number until a valid integer is provided
while True:
try:
x = int(input("Enter your Number: "))
break # Exit the loop if a valid integer is entered
except:
print("That's not a valid Number !") # Print an error message if the input is not a valid integer
print("Attempted Input !") # This line will execute after each input attempt
# # finally block
# In[5]:
# Continuously prompt the user to input a number until a valid integer is provided
while True:
try:
x = int(input("Enter your Number: "))
break # Exit the loop if a valid integer is entered
except:
print("That's not a valid Number !") # Print an error message if the input is not a valid integer
finally:
print("Attempted Input !") # This line will execute after each input attempt
# # adjust particular exception
# In[17]:
while True:
try:
x = int(input("Enter your Number: "))
break
except (ValueError, KeyboardInterrupt):
print("That's not a valid Number !")
#except ValueError:
#print("That's not a valid Number !")
#except KeyboardInterrupt:
#print("zzzz !")
#break
finally:
print("Attempted Input !")
# # Practice Quiz
# ## try n except
# In[7]:
def party_planner(cookies, people):
leftovers = None
num_each = None
# TODO: Add a try-except block here to
try:
num_each = cookies // people
leftovers = cookies % people
except ZeroDivisionError:
print("Warning! Please input a different number of people.")
return(num_each, leftovers)
# The main code block is below; do not edit this
lets_party = 'y'
while lets_party == 'y':
cookies = int(input("How many cookies are you baking? "))
people = int(input("How many people are attending? "))
cookies_each, leftovers = party_planner(cookies, people)
if cookies_each: # if cookies_each is not None
message = "\nLet's party! We'll have {} people attending, they'll each get to eat {} cookies, and we'll have {} left over."
print(message.format(people, cookies_each, leftovers))
lets_party = input("\nWould you like to party more? (y or n) ")
# In[ ]: