Returning 'incorrect' regardless [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
This has been flagged as a duplicate, i was unaware of the issue at the time but I shall take this post down in the next 48 hours, apologies.
I'm currently working on a python (v3.5.0:374f501f4567) program that generates a ranom equation using multiplication, addition or subtraction using. The code works perfectly up until the final hurdle. When trying to return if the answer is correct or incorrect I am thoroughly stumped. When I use the following code:
import random
from operator import add,sub,mul
x=random.randint(0,10)
y=random.randint(0,10)
eqn=(add, sub, mul)
eqnchoice=random.choice(eqn)
eqnstring={add:'+',sub:'-',mul:'*'}
useranswer=0
def eqngenerator():
random.seed
answer=eqnchoice(x,y)
answer=round(answer,2)
print("what's the answer to",x,eqnstring[eqnchoice],y,"=?\n")
useranswer=input("Enter the answer here:")
if useranswer==answer:
print('Correct!')
else:
print('Incorrect!')
print(eqngenerator())
I am faced with the following problem as seen in the screenshots below.
I'm befuddled as to why this is, if anyone can help please do.
Thank you for your time.

input() will be giving you a string in your useranswer variable. It will need to converted to a number before use, eg a float or an int
useranswer = int(input("Enter the answer here:"))
or
useranswer = float(input("Enter the answer here:"))
Use int if you know the answer to your calculation will always be an integer

input returns a string, so you need:
useranswer=float(input("Enter the answer here:"))
it will raise an error if user enter any other value then a number, so you can make:
def eqngenerator():
random.seed
answer=eqnchoice(x,y)
answer=round(answer,2)
print("what's the answer to",x,eqnstring[eqnchoice],y,"=?\n")
useranswer=input("Enter the answer here:")
try:
if useranswer==answer:
print('Correct!')
else:
print('Incorrect!')
except ValueError:
print('Incorrect!')

Related

I write a python program to ask my computer to guess a secret number but it does not work [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed last year.
Here is my code:
import random
def guessGame(x):
low=1
high=x
feedback=" "
while feedback != "c":
if high!=low:
guess=int(random.randint(low,high))
else:
guess=low
feedback= input(f"Is it {guess} too high(H),too lowe(L) or correct(C)?").lower()
if feedback == "h":
high = guess-1
elif feedback == "l":
low=guess+1
print(f"I guess the correct number{guess}!")
x=input("Please input a highest number in the range: ")
guessGame(x)
I wrote a python code to ask my computer to guess a secret number. But it did not work. I did some changes but still do know where I did wrong.
Python input type is a string you must convert to string to integer
x=int(input("Please input a highest number in the range: "))

Guess the Number game isn't working as intented [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed last year.
Random Number Generator - Guess It
Did a much more complex variant of this. (250+ lines) But when I tested it, even if I guessed the number correctly(it was between 1-20) it didn't accept it. Every stage, I programmed the computer to tell me a statement about the number, using if, elif and else commands. When I guessed the number, it just said the number is incorrect and told me a new statement about number. Could someone help, please? In this mini variant as well, if I get the correct answer, it still responds to the if statement.
import random
list = [1,2,3]
target = print(random.choice(list))
guess = input("What is the target number?")
if guess == target :
print("Well done.")
else:
print("The number you have guessed isn't correct.")
You need to convert the guess into an integer before comparison. and remove the print statement from the variable definition. You also need to rename the list variable as it is a protected name.
import random
listNumbers = [1,2,3]
target = random.choice(listNumbers)
guess = int(input("What is the target number?"))
if guess == target :
print("Well done.")
else:
print("The number you have guessed isn't correct.")

I am having trouble with my divide by 2 code [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I don't think I am using the write syntax to write my code, I am new and would like some help. This code is due tonight so anything would be helpful.
I have tried looking on youtube and other websites, but I couldn't find anything.
print("Hello, I can divide by two! Try me out.")
myNumber = input("What is your number?")
print("myNumber/2")
myAnswer = int(input(myNumber/2))
print("myAnswer")
I expect that the fourth line of code has incorrect syntax for the intended function.
input is always a string. It has to be converted to int for any int operations. Change this.
myNumber = input("What is your number?")
to
myNumber = int(input("What is your number?"))
You are taking input and storing it in myNumber which is string type. you can type-cast the myNumber to integer or float data type before dividing it by two.
print("Hello, I can divide by two! Try me out.")
myNumber = input("What is your number?")
print("myNumber/2")
myAnswer = int(myNumber)/2
print("myAnswer is ",myAnswer)
I believe this will do your work.

BASIC GUESSING GAME [duplicate]

This question already has answers here:
How does my input not equal the answer?
(2 answers)
Closed 5 years ago.
Hi all I hope you can explain what I am doing wrong. I am completely new to python and experimenting with basic code but it don't seem to work as i thought it might.
the program should be a simple one of guessing the right number contained in the variable, but even when guess correct it says "Nope, that's not right".
magic_number = 10
input("I am thinking of a number between 1 and 10, can you guess it? ")
if input == magic_number:
print("WOW! You must be psychic, that is spot on")
else:
print("Nope, that's not it")
first of all you should creat a variable that will store the answer, so for example.
answer = input("I am thinking of a number between 1 and 10, can you guess it? ")
thus you will also have to change if input == magic_number: to if answer == magic_number:
Nonetheless, the main problem is that when you enter something using the input method your input is automatically converted to a string.
So you have two choices:
Convert your magic number to a string (magic_number = 10to magic_number = "10") and run the code with the modifications that I have proposed
Convert your input too and Int modifying if answer == magic_number: to if int(answer) == magic_number:
Both of the methods work great, though be careful with the second one because if you input something that is not convertible to an int (for example "Hello", the code will return an error)
input isn't a variable, it's a function! You should type x=input("Enter a number"), and if x == magic_number. input returns a value, but you aren't storing the user's input anywhere.
try this :
magic_number = 10
guess = int(input("I am thinking of a number between 1 and 10, can you guess it? "))
if guess == magic_number:
print("WOW! You must be psychic, that is spot on")
else:
print("Nope, that's not it")

If Statements with Variable Numbers [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
I am working on a program and have come up with an error I don't usually face. My error is quite simple, yet I cannot wrap my head around a way to get past this problem. Thanks for helping.
Code:
budget = 50
depositAmount = input("Please enter an amount you would like to add")
if depositAmount > budget:
print("Sorry, you do not have the required funds to join. You have been exited.")
else:
#DO THE REST OF THE PROGRAM#
So in short terms, I will be adding values to the budget variable in the future, thats why I can't use the if statement and say less than 50, because the budget variable may be different. How do I make sure that if the user inputs a number greater than the variable value (or lower) they will be given an error message?
Convert depositAmount to number with int built-in before comparing with budget, to do it replace your if condition from
if depositAmount > budget:
to
if int(depositAmount) > budget:

Categories

Resources