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

Fixed with approach 3

Browse files
Week 6 Files and errors/{12. Add names and years to list → [Fixed] 12. Add names and years to list} RENAMED
@@ -238,6 +238,82 @@ List: [('Sam', 1972), ('Alex', 1904), ('Lena', 1999)]
238
  ########################################################
239
 
240
  # approach 3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
 
243
 
 
238
  ########################################################
239
 
240
  # approach 3
241
+ # added strip() to input
242
+ # moved 'len(name) < 2' check down
243
+
244
+
245
+ def main():
246
+ people = []
247
+ current_year = 2021
248
+
249
+ while True:
250
+ # Ask for name
251
+ name = input("Name: ").strip()
252
+
253
+ # Break if name is blank
254
+ if name == "":
255
+ break
256
+
257
+ continue
258
+
259
+
260
+ # Ask for year of birth
261
+ year = input("Year of birth: ").strip()
262
+
263
+
264
+ # Check if name is too short
265
+ if len(name) < 2:
266
+ print("Name's too short!")
267
+ continue # also added this, moved this below year input needed by 'autograder's expected output'
268
+
269
+ # Check if year is numerical
270
+ if not year.isdigit():
271
+ print("Year must be given in a numerical form!")
272
+ continue
273
+
274
+ year = int(year)
275
+
276
+ # Check if year is valid
277
+ if year < 1800:
278
+ print("You're too old!")
279
+ elif year > current_year:
280
+ print("You're not born yet!")
281
+ else:
282
+ people.append((name, year))
283
+
284
+ print("List:", people)
285
+
286
+ main()
287
+
288
+
289
+
290
+ actual and expected output
291
+ '''
292
+ Name: Sam
293
+ Year of birth: 1972
294
+ Name: Alex
295
+ Year of birth: 1904
296
+ Name: Lena
297
+ Year of birth: 1999
298
+ Name: A
299
+ Year of birth: 1987
300
+ Name's too short!
301
+ Name: Alex
302
+ Year of birth: 1765
303
+ You're too old!
304
+ Name: Tina
305
+ Year of birth: 2042
306
+ You're not born yet!
307
+ Name: Mark
308
+ Year of birth: in
309
+ Year must be given in a numerical form!
310
+ Name: the
311
+ Year of birth: fifties
312
+ Year must be given in a numerical form!
313
+ Name:
314
+ List: [('Sam', 1972), ('Alex', 1904), ('Lena', 1999)]
315
+
316
+ '''
317
 
318
 
319