2

I am quite new to python and programming in general. For my school task i was asked to develop a basic arithmetic quiz. However when i enter a letter instead of an integer I get a ValueError. What i wanted to do was to make the code recognize a string input as a wrong answer and i am not quite sure of how to do this. I apoligise for any messy code because it is my first time posting a question, also I have tried researching this but I had no luck as the answers that I got did not match my criteria. Here's my full code:

import random
import math
import operator as op

def test():
    #First randomly generated number
    num1 = random.randint(1, 10)
    #Second randomly generated number
    num2 = random.randint(1, num1)                                               

    #List of the signs that the program is allowed to use in order to generate the question
     ops =     {                                                                      
        '+': op.add,
        '-': op.sub,
        '*': op.mul,
    }

    ##=> ['+', '*', '-']
    keys =     list(ops.keys())                                                      
    #e.g. '+' is chosen at random
    rand_key =     random.choice(keys)                                               
    #e.g. op.add
    operation =     ops[rand_key]                                                    

    correct_result = operation(num1, num2)

    #Asks the user the randomly generated question
    print ("What is {} {} {}?".format(num1, rand_key, num2))                     
   #User inputs the answer that he/she thinks is right
    user_answer= int(input("You answered: "))                                    

    if user_answer != correct_result:
        #This is what happens when he/she gets a wrong answer
        print ("Unfortunately are wrong!. The correct answer is     {}".format(correct_result))
        return False
     else:
        #This is what happens when he/she gets a right answer
        print("Well done! You got the right answer! Here’s the next     question")                                          
    return True

#Asks the user for his/her name
username = input("What is your name?      ")                                          
print("Hi {}! Welcome to the Arithmetic quiz!".format(username))

#The amount of correct questions the user starts with (default)
correct_answers =     0                                                              
#The amount of questions that are going to be asked
num_questions =     10                                                               
#Loops the process
for i in     range(num_questions):                                                   
if test():
    correct_answers +=1


#Prints out the final score
print("{}: You got {}/{} questions     correct.".format(                                                     
#Along with the username
    username,                                                                    
#And the right number of correct answers
        correct_answers,                                                              
#Out of 10 (the original number of questions asked)
num_questions,        

))                                                      

0 Answers0