This question already has answers here:
How does my input not equal the answer?
(2 answers)
Closed 1 year ago.
I don't know why but when answering correctly, it always says it's not equal to the answers.
import random
a = random.randint(1,100)
b = random.randint(1,100)
answer = a + b
print(answer)
print(f"{a} + {b}")
urAnswer = input("Answer : ")
print("Your answer = " + urAnswer)
if urAnswer == answer:
print("You're Correct!")
else:
print(f"You answered {urAnswer}. Which wasn't the correct answer! The Correct Answer was {answer}")
The inputs supplied via input is of type string, Need to convert the input to int. Also you can do a validation for the input.
a = random.randint(1,100)
b = random.randint(1,100)
answer = a + b
print(answer)
print(f"{a} + {b}")
urAnswer = input("Answer : ")
print("Your answer = " + urAnswer)
if int(urAnswer) == answer:
print("You're Correct!")
else:
print(f"You answered {urAnswer}. Which wasn't the correct answer! The Correct Answer was {answer}")
You are comparing a string to an integer.
Make the answer an integer before comparing so the script will work
if int(urAnswer) == answer:
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
def QandA():
s_easy = open("Science (Easy).txt","r")
score = 0
for x in range (5):
print(s_easy.readline())
answer = s_easy.readline()
useranswer = input("Enter your answer as an uppercase: ")
if useranswer.isspace()or useranswer.islower():
while useranswer.isspace() or useranswer.islower():
print("You must enter a valid letter.")
useranswer = input("Enter your answer as an uppercase: ")
while useranswer != "A" or useranswer != "B":
print("You must enter a valid letter.")
useranswer = input("Enter your answer as an uppercase: ")
if answer == useranswer:
score = score + 1
else:
score = score
elif answer == useranswer:
score = score + 1
else:
score = score
print(score)
I need the while loop to ask the user to input their answer again if the useranswer is not uppercase, a space or not "A" or "B". I also need the score to increase by increments of 1 with every correct answer and to be printed at the end. I have tried to include the score, but it is just not working for me.
You need to change the or to and:
while useranswer != "A" and useranswer != "B":
#code
This is because you only wnat to prompt them again if the input isn't A and it isn't B.
The code is supposed to be a simple maths quiz, however, when I enter the correct answer, it says it's wrong. My code is:
import random
name = input("What is your name? ")
question = 0
correct = 0
while question < 10:
question = question + 1
number1 = random.randint(1, 50)
number2 = random.randint(1, 50)
print("What is", number1, "+", number2)
answer = number1 + number2
print(answer)
student = input()
if student == answer:
print("Correct! Well Done!")
correct = correct + 1
else:
print("Wrong!")
Any ideas?
You forgot to cast the user input to int, so replace student = input() with student = int(input()), as at the moment you are comparing str to int.
Assuming that you are using Python 3, you need to type cast input() to int() in order to compare it with answer which is an integer:
student = int(input())
Assuming python3. The following is not valid for python2, as input treats an integer input as integer.
input() in python2 evaluates the expression with eval() function after reading from stdin, thus returning an integer.
The problem relies on the fact you are comparing strings to answer that is an integer.
import random
name = input("What is your name? ")
question = 0
correct = 0
while question < 10:
question = question + 1
number1 = random.randint(1, 50)
number2 = random.randint(1, 50)
print("What is", number1, "+", number2)
answer = number1 + number2
print(answer)
student = input()
#Cast the input to an integer for comparison
if int(student) == answer:
print("Correct! Well Done!")
correct = correct + 1
else:
print("Wrong!")
As many have already said, you need to cast the user input to int, replace student = input()
Also, you can do question += 1, rather than question = question + 1. This can help speed you up.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
okay so im writing a code that randomly generates questions and lets the user answer but my problem is that even if the user gets the answer right it will always print incorrect
print ("what is your username")
name = input () .title()
print (name, "welcome")
import random
score=0
question=0
for i in range(10):
ops = ["+", "-", "*"]
num1 = random.randint (0,10)
num2 = random.randint (0,10)
oparator = random.choice(ops)
Q=(str(num1)+(oparator)+(str(num2)))
print (Q)
guess = input()
guess = int(guess)
if oparator =='+':
answer = (str(num1+num2))
elif oparator =='-':
answer = (str(num1-num2))
else:
oparator =='*'
answer = (str(num1*num2))
if guess == (Q):
print ("correct")
score + 1
else:
print ("incorrect")
I honestly don't understand what is wrong.
any help would be greatly thanked
p.s I know my codes a mess
You need to compare guess with answer.
print ("what is your username")
name = input().title()
print (name, "welcome")
import random
score=0
question=0
for i in range(10):
ops = ["+", "-", "*"]
num1 = random.randint (0,10)
num2 = random.randint (0,10)
oparator = random.choice(ops)
Q=(str(num1)+(oparator)+(str(num2)))
print (Q)
guess = input()
guess = int(guess)
if oparator =='+':
answer = int(str(num1+num2)) # Convert to int
elif oparator =='-':
answer = int(str(num1-num2))
else:
oparator =='*'
answer = int(str(num1*num2))
if guess == answer: # Compare user's answer with actual answer
print ("correct")
score = score + 1 # Update the score
else:
print ("incorrect")
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
Okay so I made my own Multiplication Game for my AS Computing course, but I'm running into a number of issues, mainly this occurring around this line:
if ans == result:
print ("That's right -- well done.\n")
solved = solved + 1
else:
print ("No, I'm afraid the answer is %d.\n" % result)
return solved"
The problem seems to present itself. When playing this Multiplication Game any answer that you input, seems to always be incorrect. I've posted the entire of my game below in hopes that someone can help me <3
Thanks in advance!
from random import *
solved = 0
total_num_q = 0
def play(num1, num2, type, solved):
""" The main play function"""
def sp_type():
type = input("Specify the question type: Multiplication: M, Addition :A, Subtraction: S, Division: D: ")
if type not in ['M','A','S','D']:
print("Please input only enter a valid character: ")
return type
type = ""
while type not in ['M','A','S','D']:
type = sp_type()
if type == "M":
ans = input("What's %d times %d? " % (num1, num2))
result = num1 * num2
if type == "A":
ans = input("What's %d plus %d? " % (num1, num2))
result = num1 + num2
if type == "S":
ans = input("What's %d minus %d? " % (num1, num2))
result = num1 - num2
if type == "D":
ans = input("What's %d divided by %d? " % (num1, num2))
result = num1/num2
if ans == result:
print ("That's right -- well done.\n")
solved = solved + 1
else:
print ("No, I'm afraid the answer is %d.\n" % result)
return solved
All fixed now, just took some tweaking. The issue was solved by Bruno and Chris, the error was that my inputs weren't integers, after changing the inputs, the code is working perfectly. Thanks to everyone who helped!
Not so sure, but have you checked the variable types? num1 and num2 should obviously be numerical (int?). Maybe input is string?
I am attempting to make an arithmetic quiz, but have run into this issue: Even if I input the correct answer, it seems to ignore the correct answer code and go straight to the incorrect answer code. Basically, it doesn't accept any right answers.
import random
num1 = (random.randrange(10))
num2 = (random.randrange(10))
correct1 = (num1*num2)
ans1 = input("What is " + str(num1) + " multiplied by " + str(num2) + "? ")
if ans1 == correct1:
print("Correct! ")
if ans1 != correct1:
print(" Incorrect. ")
print(" The correct answer was " + str(ans1))
When ran, I get something like this:
What is 3 multiplied by 0? 0
Incorrect.
The correct answer was 0
Note how the answer and my input were the same, but it ran the code for an incorrect answer. Can anyone help me to fix this? I am using Python 3.4.
3 is not equal to "3". The result of a call to input (in Python3) is a string, not a number.
Call int on the user input
...
ans1 = input("What is " + str(num1) + " multiplied by " + str(num2) + "? ")
ans1 = int(ans1)
...
if int(ans1) == correct1:
print("Correct! ")
else:
print(" Incorrect. ")
print(" The correct answer was " + str(ans1))
You must compare numbers with numbers.
You were comparing '3' vs 3