2

just need a little help. Starting out doing python, trying to create a high score program and I want to say while score is not equal to an integer then ask user to input score.

For example (this doesn't work)

name = input("Please enter your name: ")
score = None
while score != int(score):
    score = input("Enter your score: ")
print("congratz it worked genius")

thanks in advance

user2980960
  • 59
  • 1
  • 1
  • 3
  • Possible duplicate: http://stackoverflow.com/questions/5424716/python-how-to-check-if-input-is-a-number-given-that-input-always-returns-stri – Tom Swifty Nov 14 '13 at 17:22
  • 1. Your score remains a string so it can never equal an integer so you can never get out of your loop. 2. int(score) raises ValueError for any string which is not containing an integer, so your bad inputs will result an exception. Check Nicolas' answer. – SzieberthAdam Nov 14 '13 at 18:15

1 Answers1

5
 while True:
    try:
        score = int(input("Enter your score: "))
        break
    except ValueError:
        print("Int, please.")
Nicolas
  • 5,583
  • 1
  • 25
  • 37