2

I'm currently creating a quiz to test children's ability to add, subtract and multiply two numbers together. You can see the code below as to what I have developed so far:

from random import randint
import re

first_number = randint(30,60)
second_number = randint(30,60)
correct_ans = str(first_number - second_number)

while True:
    user_input = input("%s - %s = " %(first_number, second_number))
    if re.search(r'[\d]', user_input):
        print("Error. You have entered a non - integer character. The question will be asked again.")
    elif re.search(r'[0-9]', user_input):
        if (user_input == correct_ans):
            print("Correct! Well done!")
            break
        else:
            print("Your answer is incorrect. The correct answer was %s" %(correct_ans))
            break

To explain the program briefly -
There are two random numbers generated, and this function ONLY multiplies the numbers together. A while loop asks the user for the correct result. Now, the first regular expression analyses the input to see if they have entered any non - digit characters (punctuation, letters) etc. If they have, they receive a message and the question is asked again.

Now, the second part of the if statement then checks to see that if they have entered a number, and if they have, then the program checks to see if the input is correct to the correct_ans. If it is, they receive a message saying well done! and the program breaks. If they get it wrong, the program outputs a message and they still get it wrong.

The issue that I'm having is that the IDLE is outputting the first message "Error, you have entered..." all the time, like this:

50 - 60 = -10
Error. You have entered a non - integer character. The question will be asked again.
50 - 60 = 

How can I solve this? And more importantly, how can I get the computer to accept negative values for the subtraction operation, since it has a '-' and considers it a punctuation?

1
  • Hint: '10'.isdigit() and abs(-10) Commented Dec 22, 2014 at 11:11

3 Answers 3

1

Just use a try/except which will handle negative numbers also:

from random import randint


first_number = randint(30,60)
second_number = randint(30,60)
correct_ans = first_number - second_number

while True:
    user_input = input("%s - %s = " %(first_number, second_number))
    try:
        user_input = int(user_input)
    except ValueError:
        print("Error. You have entered a non - integer character. The question will be asked again.")        
    else:
        if user_input == correct_ans:
            print("Correct! Well done!")
            break
        else:
            print("Your answer is incorrect. The correct answer was %s" %(correct_ans))
            break
Sign up to request clarification or add additional context in comments.

Comments

1

I believe your regular expression is only looking for ONE digit. You should look into using repetition with your regex.

However, since you are looking for integer values, I think an easier way to do this would be to use Python's int function.

user_input = input("%s - %s = " %(first_number, second_number))
try:
    user_input = int(user_input)
except ValueError e:
    print("Error. You have entered a non - integer character. The question will be asked again.")

int accepts strings integer strings (including plus and minus signs, and ignoring whitespace) and is a much more robust way to validate integer input.

Comments

0

I'm not quite sure what you think the first regex is doing. In fact, it is doing exactly the same thing as the second one, since [\d] is the same as [0-9].

You could use [^\d-] instead, to check that what you have is not a digit or the - character, but what you actually want is to check whether the value is a valid integer, which you can do by using the int function directly and catching the exception, as others have pointed out.

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.