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

2
  • Possible duplicate: stackoverflow.com/questions/5424716/… Commented Nov 14, 2013 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. Commented Nov 14, 2013 at 18:15

1 Answer 1

5
 while True:
    try:
        score = int(input("Enter your score: "))
        break
    except ValueError:
        print("Int, please.")
Sign up to request clarification or add additional context in comments.

1 Comment

Why does the question code not recognize an input even if it IS an int?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.