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")
Related
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: "))
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.")
This question already has answers here:
How do I convert all strings in a list of lists to integers?
(15 answers)
Closed 4 years ago.
Compare two variables named target and guess when one is an integer, and one is a string using Python 3.
import random
import sys
target = random.randint(1, 20)
name = input ('Hello, what is your name?')
print ('Hello, %s. I am thinking of a number from 1 to 20. You will have 3 tries, and after each try, I will tell you if the number that I am thinking of is lower or higher. Try to guess it!' % name)
guess = input ('What number do you think I am thinking of?')
if guess == target:
print ('Congratulations! You won! Please play again!')
sys.exit()
else:
print ('You did not guess the number correctly.')
if target < guess:
print ('The number that I am thinking of is smaller than your guess. Try again')
else:
print ('The number that I am thinking of is larger than your guess. Try again!')
You can simply parse the input from string to an integer in this manner:
guess = int(input('What number do you think I am thinking of?'))
And then you can freely compare it to any integer you'd like.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
I am taking in intro CS course and am playing around with some of my old code from an early lab. The program asks for four separate inputs from the user and will then spit out a graph according to the four inputs. I'm trying to get it where if the user does not enter a number then it asks for them to try again and prompt for another input. I've tried things like:
(if x != int:) and (if x =\= int:) but nothing has worked for me.
You could do something like this:
userInput = 0
while True:
try:
userInput = int(input("Enter an integer: "))
except ValueError:
print("Not an integer!")
continue
else:
print("Yay an integer!")
break
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!')