Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Looping a Program
#1
(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:
        break
This 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.
Reply


Messages In This Thread
[split] Looping a Program - by volcano63 - Oct-05-2018, 09:26 PM
RE: [split] Looping a Program - by micseydel - Oct-09-2018, 12:26 AM
RE: Looping a Program - by perfringo - Oct-06-2018, 05:12 AM
RE: Looping a Program - by volcano63 - Oct-06-2018, 08:31 AM
RE: Looping a Program - by perfringo - Oct-07-2018, 09:00 AM
RE: Looping a Program - by volcano63 - Oct-07-2018, 09:26 AM
RE: Looping a Program - by perfringo - Oct-08-2018, 11:31 AM
RE: Looping a Program - by stullis - Oct-08-2018, 02:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Results of this program in an excel file eisamabodian 1 1,610 Feb-11-2022, 03:18 PM
Last Post: snippsat
  Need help looping this program JakobeTheKid 1 2,128 May-19-2019, 05:30 AM
Last Post: SheeppOSU
  Looping a Program DavidRobinsons 4 3,588 Oct-09-2018, 12:14 AM
Last Post: micseydel
  [split] Coin Flip Program Crackity 5 4,894 Sep-25-2017, 03:48 AM
Last Post: Crackity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020