Skip to content Skip to sidebar Skip to footer

How Do I Get The User To Input A Number In Python 3?

I'm trying to make a quiz using Python 3. The quiz randomly generates two separate numbers and operator. But when I try to get the user to input their answer, this shows up in the

Solution 1:

This line is incorrect:

ifinput(int)==(num1,op,num2):

You must convert the input to int and apply op to num1 and num2:

ifint(input()) == op(num1, num2):

Solution 2:

You almost had it working. The reason for the error was you were telling the input command to display an int as a prompt, rather than converting the returned value into an int.

Secondly your method for calculating the answer needed fixing as follows:

import random
import operator

operation=[
    (operator.add, "+"),
    (operator.mul, "*"),
    (operator.sub, "-")
    ]

num_of_q = 10
score = 0

name=input("What is your name? ")
class_name=input("Which class are you in? ")
print(name,", welcome to this maths test!")

for _ in range(num_of_q):
    num1=random.randint(0,10)
    num2=random.randint(1,10)
    op, symbol=random.choice(operation)
    print("What is",num1,symbol,num2,"?")

    if int(input()) == op(num1, num2):
          print("Correct")
          score += 1else:
          print("Incorrect")

print(name,"you got",score,"/",num_of_q)

Post a Comment for "How Do I Get The User To Input A Number In Python 3?"