(Oct-05-2018, 07:36 PM)perfringo Wrote: One way of doing it:
while True: print("Enter the name and scores of your students.") name = input("Enter name: ") while True: try: score = int(input("Enter score: ")) if score in range(6): break else: print('Only numbers 0 to 5 are acceptable!') except ValueError: print('Only numbers 0 to 5 are acceptable!') for i, v in enumerate(['F', 'E', 'D', 'C', 'B', 'A']): if i == score: grade = v print(f'{name} got a {grade}') repeat = input('Would you like to continue? Y/N ').lower() if repeat == 'y': continue else: breakThis code will repeat only when user enter either 'Y' or 'y'. All other entries will end the program
-
Your solution contains redundant loop and else's
else
on line 9 is redundant
- Lines 14-16 may be replaced by
grade = ['F', 'E', 'D', 'C', 'B', 'A'][score]
- Line 20-23 may be rewritten as
if repeat != 'y': break
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.