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 shell:

<class 'int'> 

I'm not sure what I need to do. Even if I type in the correct answer, it always returns as incorrect.

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 input(int)==(num1,op,num2):

          print("Correct")
          score += 1
    else:
          print("Incorrect")

if num_of_q==10:

        print(name,"you got",score,"/",num_of_q)
6
  • 1
    Do you want if int(input('Enter the number: '))==(num1,op,num2): Commented Sep 20, 2015 at 12:49
  • @KevinGuan that should be an answer, that is probably what OP is looking for Commented Sep 20, 2015 at 12:50
  • @PreetKukreti Thanks, let me post an answer ;) Commented Sep 20, 2015 at 12:52
  • I tried this and it works, but the answers still come up with incorrect. Commented Sep 20, 2015 at 12:54
  • Try @MichaelLaszlo 's answer :) Commented Sep 20, 2015 at 12:54

2 Answers 2

7

This line is incorrect:

if input(int)==(num1,op,num2):

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

if int(input()) == op(num1, num2):
Sign up to request clarification or add additional context in comments.

2 Comments

Furthermore, input(x) uses x as the message to print, where x is implicitly converted to a str. int by itself is the name of a type. A type's str is usually its repr and for int, that returns the string "<class 'int'>". This indicates why you were getting that message.
@CyberSkull311 Please remember accept the correct answer.
1

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 += 1
    else:
          print("Incorrect")

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

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.