KaiquanMah commited on
Commit
fdc8607
·
verified ·
1 Parent(s): b7926b9

Create Week 6 Files and errors/12. Add names and years to list

Browse files
Week 6 Files and errors/12. Add names and years to list ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ Write a program that asks the user for their name and year of birth.
3
+
4
+ If there is no error in the name or year, they are added to the tuple list.
5
+ When the user replaces the name with an empty string, the program prints the list and terminates.
6
+
7
+ See the sample output below for a description of how the program should react to various error situations.
8
+ The earliest accepted year of birth is 1800, the minimum length of the name is 2 characters.
9
+ The program assumes that the year is 2021 - so the person cannot have been born after that date.
10
+
11
+ Name: Alex
12
+ Year of birth: 1904
13
+ Name: Lena
14
+ Year of birth: 1999
15
+ Name: A
16
+ Year of birth: 1987
17
+ Name's too short!
18
+ Name: Alex
19
+ Year of birth: 1765
20
+ You're too old!
21
+ Name: Tina
22
+ Year of birth: 2042
23
+ You're not born yet!
24
+ Name: Mark
25
+ Year of birth: in the fifties
26
+ Year must be given in a numerical form!
27
+ Name:
28
+ List: [('Alex', 1904), ('Lena', 1999)]
29
+ '''
30
+
31
+
32
+
33
+ # approach 1
34
+ def main():
35
+ people = []
36
+ current_year = 2021
37
+
38
+ while True:
39
+ name = input("Name: ")
40
+
41
+
42
+ if name == "":
43
+ break
44
+
45
+ if len(name) < 2:
46
+ print("Name's too short!")
47
+ continue
48
+
49
+
50
+
51
+
52
+ year = input("Year of birth: ")
53
+
54
+ if not year.isdigit():
55
+ print("Year must be given in a numerical form!")
56
+ continue
57
+
58
+
59
+ year = int(year)
60
+
61
+
62
+ if year < 1800:
63
+ print("You're too old!")
64
+ elif year > current_year:
65
+ print("You're not born yet!")
66
+ else:
67
+ people.append((name, year))
68
+
69
+ print("List:", people)
70
+
71
+ main()
72
+
73
+
74
+
75
+
76
+ '''Error
77
+
78
+ Error in program execution
79
+ line 68, in
80
+ main()
81
+ File "/tmp/untrusted/test08c6d0c3d-26b8-4214-8bab-fff48628c30b/test.py", line 35, in main
82
+ name = input("Name: ")
83
+ File "/tmp/untrusted/test08c6d0c3d-26b8-4214-8bab-fff48628c30b/test.py", line 17, in input
84
+ raise Exception("The program asked for too many values in the input statement! The maximum number of values to ask for was "
85
+ Exception: The program asked for too many values in the input statement! The maximum number of values to ask for was 17
86
+ '''
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+ ########################################################
98
+
99
+ # approach 2
100
+
101
+
102
+ def main():
103
+ people = []
104
+ current_year = 2021
105
+
106
+ while True:
107
+ # Ask for name and year in a single input
108
+ user_input = input("Name and year of birth (or leave blank to exit): ").strip()
109
+
110
+
111
+ if user_input == "":
112
+ break
113
+
114
+ # Split input into name and year
115
+ parts = user_input.split()
116
+ if len(parts) < 2:
117
+ print("Please provide both name and year of birth!")
118
+ continue
119
+
120
+ name = parts[0]
121
+ year = parts[1]
122
+
123
+
124
+
125
+ if len(name) < 2:
126
+ print("Name's too short!")
127
+ continue
128
+
129
+
130
+ if not year.isdigit():
131
+ print("Year must be given in a numerical form!")
132
+ continue
133
+
134
+
135
+ year = int(year)
136
+
137
+
138
+ if year < 1800:
139
+ print("You're too old!")
140
+ elif year > current_year:
141
+ print("You're not born yet!")
142
+ else:
143
+ people.append((name, year))
144
+
145
+ print("List:", people)
146
+
147
+
148
+ main()
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+ '''actual output
159
+ Name and year of birth (or leave blank to exit): Sam
160
+ Please provide both name and year of birth!
161
+ Name and year of birth (or leave blank to exit): 1972
162
+ Please provide both name and year of birth!
163
+ Name and year of birth (or leave blank to exit): Alex
164
+ Please provide both name and year of birth!
165
+ Name and year of birth (or leave blank to exit): 1904
166
+ Please provide both name and year of birth!
167
+ Name and year of birth (or leave blank to exit): Lena
168
+ Please provide both name and year of birth!
169
+ Name and year of birth (or leave blank to exit): 1999
170
+ Please provide both name and year of birth!
171
+ Name and year of birth (or leave blank to exit): A
172
+ Please provide both name and year of birth!
173
+ Name and year of birth (or leave blank to exit): 1987
174
+ Please provide both name and year of birth!
175
+ Name and year of birth (or leave blank to exit): Alex
176
+ Please provide both name and year of birth!
177
+ Name and year of birth (or leave blank to exit): 1765
178
+ Please provide both name and year of birth!
179
+ Name and year of birth (or leave blank to exit): Tina
180
+ Please provide both name and year of birth!
181
+ Name and year of birth (or leave blank to exit): 2042
182
+ Please provide both name and year of birth!
183
+ Name and year of birth (or leave blank to exit): Mark
184
+ Please provide both name and year of birth!
185
+ Name and year of birth (or leave blank to exit): in
186
+ Please provide both name and year of birth!
187
+ Name and year of birth (or leave blank to exit): the
188
+ Please provide both name and year of birth!
189
+ Name and year of birth (or leave blank to exit): fifties
190
+ Please provide both name and year of birth!
191
+ Name and year of birth (or leave blank to exit):
192
+ List: []
193
+
194
+ '''
195
+
196
+
197
+
198
+
199
+
200
+ '''expected output
201
+ Name: Sam
202
+ Year of birth: 1972
203
+ Name: Alex
204
+ Year of birth: 1904
205
+ Name: Lena
206
+ Year of birth: 1999
207
+ Name: A
208
+ Year of birth: 1987
209
+ Name's too short!
210
+ Name: Alex
211
+ Year of birth: 1765
212
+ You're too old!
213
+ Name: Tina
214
+ Year of birth: 2042
215
+ You're not born yet!
216
+ Name: Mark
217
+ Year of birth: in
218
+ Year must be given in a numerical form!
219
+ Name: the
220
+ Year of birth: fifties
221
+ Year must be given in a numerical form!
222
+ Name:
223
+ List: [('Sam', 1972), ('Alex', 1904), ('Lena', 1999)]
224
+
225
+ '''
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+ ########################################################
239
+
240
+ # approach 3
241
+
242
+
243
+
244
+