Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a Code (Newbie)
#1
Hello every one, im a newbie python programmer.I was just trying few codes the other day and came across a simple code, unfortunately its stuck in an infinite loop. I tried to figure it out but for no avail.It is basically a Score storing code which shows , adds , removes,sorts and exits based on users will. Down is the code im trying to run but the infinite loop problem arises.
-----------------------------------------------------------------------------------------------
scores = []

choice = None

while choice != 0:
    print("""
\t\t High Score

0 - Exit
1 - Show Scores
2 - Add scores
3 - Remove a Score
4 - Sort scores
""")

choice = input("Choice: ")

#Programe ends when user enters '0'
if choice == 0:
    print ('Good Bye')

#All the stored High scores are printed
elif choice == 1:
    print ('High Score')
    for score in scores:
        print (score)
        
#Lets user enter the High Score
elif choice == 2:
    score = int(input("What score did you get"))
    scores.append(score)
    
#Removes a Score
elif choice == 3:
    score = ("Remove which score?")
    if score in scores :
        score.remove(score)
        
else:
    print(score,'isnt in the score list')
-------------------------------------------------------------------------------------------
Oh and the indentations are proper, its just that when i post the thread, it automatically cancel out unwanted space.
Reply
#2
input will return str, you need to convert it to int in order to compare with 1, 2, or 3
Or you need to compare user input with a str, i.e. '1', '2' or '3'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie - code solution explained Stjude1982 2 1,790 Sep-16-2021, 08:54 AM
Last Post: Stjude1982
  newbie need help update code Mariaa 3 2,028 Oct-02-2020, 08:12 AM
Last Post: ibreeden
  I am a newbie.Help me with this simple piece of code feynarun 3 2,752 Jan-08-2020, 12:40 PM
Last Post: perfringo
  complete newbie, trying to tweak some code notarobot 6 3,116 Nov-02-2019, 03:35 PM
Last Post: notarobot
  Newbie: Help with code related to Caesar Cipher jessiblah 2 3,363 May-15-2018, 04:28 PM
Last Post: nilamo
  Newbie here. What's wrong with my code? GalaxyCR 3 3,369 Sep-29-2017, 01:51 PM
Last Post: buran

Forum Jump:

User Panel Messages

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