-4

I have finished a code, so that it asks the user to answer an arithmetic question and tell them if their answer is correct or not and so on.... I started doing some tests and realised if user enters anything then a number is gives me an error. My Code:

import random

    name=input("Welcome to this Arithmetic quiz,please enter your name:")

    score = 0

    for i in range(10):

          number1=random.randint(20,50)

          number2=random.randint(1,20)

          oper=random.choice('+-*')

          correct_answer = eval(str(number1)+oper+str(number2))

          answer = (int(input('What is:'+str(number1)+oper+str(number2)+'=')) == correct_answer)

          if answer:
                  print('Correct!')

                  score += 1

          else:
                  print('Incorrect!')

    print(name,"You got",score,"out of 10")

    if score>1 and score<=3 :
                  print('Practice More!')

    elif score>4 and score<=7 :
                  print('You did well!')

    elif score>7 and score<=9 :
                  print('Excellent!')

    elif score==10 :
                  print('You are a Genius!')

    else:
                  print('Have you tried your best?')

I want to know how do I repeat line 9 until the user enters a number? THIS IS NOT A DUPLICATE BECUASE I WANT THE USER TO SPECIFICALLY ENTER A NUMBER. IF HE/SHE DID IT WILL TELL THEM IT IS WRONG OR RIGHT AND MOVE ON TO THE NEXT QUESTION.

PythonNooby
  • 33
  • 1
  • 7
  • You may use `isnumeric()` as condition of a `while` loop to check, if the input only contains numbers. – AdmPicard Jan 09 '16 at 16:22
  • Can you please show me how to do that ? – PythonNooby Jan 09 '16 at 16:37
  • Of course, just use `while not string.isnumeric():` as the loop and inside of it set `string = input('Provide a number: ')`. Here `string` has to be present before and must not be a number already. – AdmPicard Jan 09 '16 at 16:48
  • it says string is not defined – PythonNooby Jan 09 '16 at 17:04
  • Split your `answer=` line into collecting the input (within the `while` loop where `answer` replaces `string` in my example) and evaluating the correct answer after the loop, when the user entered a valid number. Also set `answer=""` before the loop. So repeat the input until the user provides a number and compare to `correct_answer` afterwards. – AdmPicard Jan 09 '16 at 17:12
  • can you just edit my code please. – PythonNooby Jan 09 '16 at 17:22
  • Please take a look here: https://codeshare.io/RQ3kM – AdmPicard Jan 09 '16 at 17:30
  • OMGG !!!! I love u man (no homo) , I really appreciate your answer , thank you 100 times :) – PythonNooby Jan 09 '16 at 18:00
  • Glad, that it works. The linked duplicate provides good hints as well concerning validating inputs. – AdmPicard Jan 09 '16 at 18:06
  • quick question is 'answer.isnumeric()' a function ? – PythonNooby Jan 09 '16 at 18:09
  • It is a [method](https://docs.python.org/2/library/stdtypes.html#unicode.isnumeric) for unicode strings. You could use `'test'.isnumeric()` as well, which returns `False` - in this case `answer` was the string. – AdmPicard Jan 09 '16 at 18:15

1 Answers1

0

I would recommend putting the for loop in a while loop until true then exit the loop. Did that answer your question?

  • Thanks for reply, but that means the user will just keep on going until he gets it right which is not fair , I only need it to repeat if the user does not enter a number , is that possible ? – PythonNooby Jan 09 '16 at 16:13