Game Quiz issues
Question:
my program keeps returning a score of 0 everytime I run it even with the corrcect answer. Also how would I go about subtractiong points for a wrong answer.
P.S, This is my first time coding anything.
score = 0
print('Math Quiz')
def check_answer(question, answer):
global score
if question == answer:
print("Correct answer!")
score = score + 1
else:
print('Sorry, wrong answer.')
question_1 = input('What is 2 times 2?n')
check_answer(question_1, 4)
question_2 = input('What is 1 minus 1?n')
check_answer(question_2, 0)
question_3 = input('What is 4 divided by 2?n')
check_answer(question_3, 2)
question_4 = input('What is 3 to the third power?n')
check_answer(question_4, 9)
question_5 = input('What is 3 times 5?n')
check_answer(question_5, 15)
print(score)
Answers:
The problem is, that input()
returns a string which leads to an incompatible comparison of your data types. You could solve this by creating an integer with the value of your input like this: question_1 = int(input('What is 2 times 2?n'))
the other problem could be solved by decreasing the score in your else block
input(”) returns a string and you are comparing it to a int.
check_anser(int(question_2),0)
or comparte to string version of the numbers
check_anser(question_2,"0")
my program keeps returning a score of 0 everytime I run it even with the corrcect answer. Also how would I go about subtractiong points for a wrong answer.
P.S, This is my first time coding anything.
score = 0
print('Math Quiz')
def check_answer(question, answer):
global score
if question == answer:
print("Correct answer!")
score = score + 1
else:
print('Sorry, wrong answer.')
question_1 = input('What is 2 times 2?n')
check_answer(question_1, 4)
question_2 = input('What is 1 minus 1?n')
check_answer(question_2, 0)
question_3 = input('What is 4 divided by 2?n')
check_answer(question_3, 2)
question_4 = input('What is 3 to the third power?n')
check_answer(question_4, 9)
question_5 = input('What is 3 times 5?n')
check_answer(question_5, 15)
print(score)
The problem is, that input()
returns a string which leads to an incompatible comparison of your data types. You could solve this by creating an integer with the value of your input like this: question_1 = int(input('What is 2 times 2?n'))
the other problem could be solved by decreasing the score in your else block
input(”) returns a string and you are comparing it to a int.
check_anser(int(question_2),0)
or comparte to string version of the numbers
check_anser(question_2,"0")