Arthimatic Quiz Not Accepting Correct Answers

2024/10/5 14:56:18

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? 0Incorrect. 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.

Answer

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)...
https://en.xdnf.cn/q/120272.html

Related Q&A

Linux - Check if python script is running in screen and run if not [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

Making Phonebook in python : i want to get this screen by fixing my current code

I made my code like below....But as i input the data such as spam & number, previous data is deleted.So id like to make multiple value in one key... (i think using list is kinda good method)For exa…

Adding enemies to a pygame platformer

Im new to pygame and trying to make a platformer game thats based on this tutorial: http://programarcadegames.com/python_examples/show_file.php?file=platform_scroller.pyI cant quite figure out how to …

Tips for cleaning up a challenges answer? Weighted Sum of Digits [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Get a variable as filename from python script and use it in a batch script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 3.x AttributeError: NoneType object has no attribute groupdict

Being a beginner in python I might be missing out on some kind of basics. But I was going through one of the codes from a project and happened to face this :AttributeError: NoneType object has no attri…

importing images from local folder instead of using Keras/Tensorflow dataset

Hi Can someone please help me to change this code so that it wont get data from keras mnist. instead it will be getting data from local folder. where do i need to make changes in it. and where in this …

why I have negative date by subtraction of two column?

Im trying to create a column his values is the subtraction of two column but I found strange values:Patient["Waiting"] = Patient["Appointment"] - Patient["Scheduled"]Sched…

Python 3: AttributeError: int object has no attribute choice [closed]

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 repro…