0

I am trying to make a maths quiz with Python and to do this I wanted to make a validation rule that meant if a user put something that was not a number, it will ask them again as it is invalid.

I tried to do this and I am coming up with the same error as I would if I did it normally. Any help?

if operator=="+":
   #this is a if statement which states that if the operator is
  #add then the answer should be num1 add num2 as the add function was     randomly
    #picked
    def inputNumber(message):
        while True:
            try:
                useranswer=int(input(actualquestion))
            except ValueError:
                print("This is not an integar!")
                continue
            else:
                 return usseranswer
                 break
actualanswer=num1 + num2 #this states that the answer is equal to the rando
#ly picked num1 add num2 as the operator is add.
if useranswer==actualanswer:
    #this if function states that if the users answer is equal to the real answer
    #the programme worked out before hand.
    score+=1 #if the answer is right you will add 1 to the score
    questions+= 1 #you will add 1 to the question also as 1 qw has been asked.
1
  • "I am coming up with the same error" And that error is? Commented Mar 15, 2016 at 20:50

1 Answer 1

1

Your function would normally go at the top of your other code and would not need to be indented:

def inputNumber(message):
    while True:
        try:
            return int(input(message))
        except ValueError:
            print("This is not an integer!")

result = inputNumber("Please enter a number: ")
print(result)

For example the following should do what you need:

Please enter a number: hello
This is not an integer!
Please enter a number: 123
123
Sign up to request clarification or add additional context in comments.

Comments

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.