0

This is my code which is a quiz for children. What I need help on is, after the code asks the user for its class, it should validates whether the input is valid or not straight away instead of doing it in the end and displaying the message "Sorry, we can not save your data as the class you entered is not valid." I've tried moving the whole if, elif, and else statements to straight after:

users_class = int(input("Which class are you in? (1,2 or 3)"))

But that doesn't help. Any help will be appreciated :)

import time
import random
import math
import operator as op

def test():
    num1 = random.randint(1, 10)
    num2 = random.randint(1, num1)

    ops = {
        '+': op.add,  
        '-': op.sub,
        '*': op.mul,
        }

    keys = list(ops.keys()) 
    rand_key = random.choice(keys)  
    operation = ops[rand_key]  

    correct_result = operation(num1, num2)

    print ("What is {} {} {}?".format(num1, rand_key, num2))
    user_answer= int(input("Your answer: "))

    if user_answer != correct_result:
        print ("Incorrect. The right answer is {}".format(correct_result))
        return False
    else:
        print("Correct!")
        return True

username=input("What is your name?")

print ("Hi {}! Wellcome to the Arithmetic quiz...".format(username))

users_class = int(input("Which class are you in? (1,2 or 3)"))

input("Press Enter to Start...")
start = time.time()

correct_answers = 0
num_questions = 10

for i in range(num_questions):
    if test():
        correct_answers +=1

print("{}: You got {}/{} {} correct.".format(username, correct_answers,  num_questions,
'question' if (correct_answers==1) else 'questions'))

end = time.time()
etime = end - start
timeTaken = round(etime)

print ("You completed the quiz in {} seconds.".format(timeTaken))

if users_class == 1:
    with open("class1.txt","a+") as f:
        f.write("   {}:Scored {} in {} seconds.".format(username,correct_answers,timeTaken))

elif users_class == 2:
    with open("class2.txt","a+") as f:
        f.write("   {}:Scored {} in {} seconds.".format(username,correct_answers,timeTaken))

elif users_class == 3:
    with open("class3.txt","a+") as f:
        f.write("   {}:Scored {} in {} seconds.".format(username,correct_answers,timeTaken))
else:
print("Sorry, we can not save your data as the class you entered is not valid.")
Hasiba
  • 3
  • 5

3 Answers3

0

You just need to add an extra condition following the question about which class the user is in. Normally we would have a loop here so that the user is asked again if they enter an invalid response.

print ("Hi {}! Welcome to the Arithmetic quiz...".format(username))
while True:
    users_class = int(input("Which class are you in? (1,2 or 3)"))
    if users_class in [1, 2, 3]:
        break
    print('Please make sure you enter a class from 1 to 3')
Stuart
  • 9,597
  • 1
  • 21
  • 30
0

You should check that the input is valid immediately after the input, something like this:

import sys

users_class = int(input("Which class are you in? (1,2 or 3)"))

if users_class not in {1,2,3}:
    print("Sorry, you must enter either a 1, 2, or 3!")
    sys.exit(1)

Note that the sys.exit(1) call will exit the program.

Now, this isn't the most robust way to do things. If the user inputs something other than a number, for example, int(...) will raise an exception, because it can't convert, say, "dog", to an integer. Furthermore, you might prefer that the program continue asking for valid input, rather than just stopping and exiting. Here's some code that will do just that:

while True:
    try:
        # try to convert the user's input to an integer
        users_class = int(input("Which class are you in? (1,2 or 3)"))
    except ValueError:
        # oh no!, the user didn't give us something that could be converted
        # to an int!
        print("Please enter a number!")
    else:
        # Ok, we have an integer... is it 1, 2, or 3?
        if users_class not in {1,2,3}:
            print("Please enter a number in {1,2,3}!")
        else:
            # the input was 1,2, or 3! break out of the infinite while...
            break

print(users_class)
jme
  • 19,895
  • 6
  • 41
  • 39
  • Thanks! works great. But last question. How do I only allow the user to enter an integers during the quiz? – Hasiba Jan 25 '15 at 22:14
  • @Hasiba Same idea, just without the `if users_class not in ...` part. See [this answer](http://stackoverflow.com/a/23294659/1231929) for a detailed explanation. Since you'll be doing this multiple times throughout your program, it might be a good idea to make the input validating code into a function. That link gives an example of such. – jme Jan 25 '15 at 22:45
0

You need to check whether the value is 1, 2 or 3 straight after you ask them, so, you can move the if's directly after the users_class = int(input("Which class are you in? (1,2 or 3)")).

Move the correct_answers=0 above the if's and set timeTaken to 0 before.

This works:

import time
import random
import math
import operator as op
import sys

def test():
    num1 = random.randint(1, 10)
    num2 = random.randint(1, num1)

    ops = {
        '+': op.add,  
        '-': op.sub,
        '*': op.mul,
        }

    keys = list(ops.keys()) 
    rand_key = random.choice(keys)  
    operation = ops[rand_key]  

    correct_result = operation(num1, num2)

    print ("What is {} {} {}?".format(num1, rand_key, num2))
    user_answer= int(input("Your answer: "))

    if user_answer != correct_result:
        print ("Incorrect. The right answer is {}".format(correct_result))
        return False
    else:
        print("Correct!")
        return True

username=input("What is your name?")

print ("Hi {}! Wellcome to the Arithmetic quiz...".format(username))

users_class = int(input("Which class are you in? (1,2 or 3)"))

correct_answers = 0
timeTaken = 0

if users_class == 1:
    with open("class1.txt","a+") as f:
        f.write("   {}:Scored {} in {} seconds.".format(username,correct_answers,timeTaken))

elif users_class == 2:
    with open("class2.txt","a+") as f:
        f.write("   {}:Scored {} in {} seconds.".format(username,correct_answers,timeTaken))

elif users_class == 3:
    with open("class3.txt","a+") as f:
        f.write("   {}:Scored {} in {} seconds.".format(username,correct_answers,timeTaken))
else:
    print("Sorry, we can not save your data as the class you entered is not valid.")
    sys.exit(0);

input("Press Enter to Start...")
start = time.time()

num_questions = 10

for i in range(num_questions):
    if test():
        correct_answers +=1

print("{}: You got {}/{} {} correct.".format(username, correct_answers,  num_questions,
'question' if (correct_answers==1) else 'questions'))

end = time.time()
etime = end - start
timeTaken = round(etime)

print ("You completed the quiz in {} seconds.".format(timeTaken))

If it's invalid, then quit via sys, otherwise continue as your current code does.

ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38