This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I'm new to Yython programming. I create a simple program with random module, that ask for number, and person need to guess an number. I got problem with getting the answer. Even if I give the correct answer, program isn't stopping, here's the code:
import random
run = True
answer = random.randint(1,9)
guess = input("Give me an number in 1 to 9: ")
print(answer)
while run:
if guess == answer:
print("Congratulations, you won!\n" * 5)
run = False
else:
guess = input("Try again: ")
print(answer)
The print(answer) line is for me to know what is the answer, and even if I write it down, program isn't stopping.
answer is always an integer:
answer = random.randint(1,9)
and guess is always a string:
guess = input("Give me an number in 1 to 9: ")
thus they can never be equal.
You need to conver the inputted string to an integer:
guess = int(input("Give me an number in 1 to 9: "))
Or better yet, convert the generated random number to a string, to avoid the issue of the program crashing when the user inputs a non digit:
answer = str(random.randint(1,9))
The random function will return an integer and the input function will return a string in python, "1" is not equal to 1. To be able to check if the input is the same, convert the random number to a string by doing guess == str(answer) instead
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
I'm putting together a small program for a friend that requires an input from the user, and depending on the input it does a certain function
Heres my code:
value = input ("Enter Number")
if value == 1:
print("You entered 1")
elif value == 2 :
print("You ented 2!")
else:
print("hmmm")
However, even entering 1 or 2, it always prints "hmmm".
I've tried everything including making a new function and passing the input into it and still it doesn't take. Any advice?
That's because you are taking input as a string not an integer.
Because of it your value is string and when it is compared with integer 1 or 2 it's coming false and the else condition gets satisfied.
Correct code:
value = int(input ("Enter Number"))
if value == 1:
print("You entered 1")
elif value == 2 :
print("You ented 2!")
else:
print("hmmm")
This question already has answers here:
Numeric comparison with user input always produces "not equal" result
(4 answers)
Closed 4 years ago.
whats up? I'm playing around with my mastermind project for school, only recently started dabbling into Python - and I've ran into a problem I simply cannot figure out? I've looked at other people's questions, who seem to have the same problem as me, but it seems to be more selective, and my code is kind of different. Can anyone tell me why whenever I reply to the question, it immediately skips to "Try again!", even if I know for a fact rnumber == tnumber? (Using Python 3.4.2).
#Generates the random number module
import random
#Creates the variable in which I store my random number
rnumber = random.randint(0,9999)
#Delete this code when complete
print (rnumber)
#Number of tries
numot = 0
#Asks for user input, on what they think the number is
tnumber = input("Guess the four digit number. ")
type(tnumber)
#Compares their number to the random number
if tnumber == rnumber:
print ("You win!")
elif rnumber != tnumber:
print ("Try again!")
numot = numot+1
you need to make your input an int so it considers it a number, try this
#Generates the random number module
import random
#Creates the variable in which I store my random number
rnumber = random.randint(0,9999)
#Delete this code when complete
print (rnumber)
#Number of tries
numot = 0
#Asks for user input, on what they think the number is
tnumber = int(input("Guess the four digit number. "))
#Compares their number to the random number
if tnumber == rnumber:
print ("You win!")
else rnumber != tnumber:
print ("Try again!")
numot = numot+1
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
Another newbie question:
I'm trying to add a statement inside a while loop that if the person enters anything except integer it will repeat the input but I didn't figure out how to do that without ruining the program. Whenever I enter anything i get the following error: "ValueError: invalid literal for int() with base 10" What is needed to be added to my code?
Here is my code:
import random
#Playing dice game against the computer
num = int(input("Enter a number between 1 and 6 please: "))
while not int(num) in range(1, 7):
num = int(input("Please choose a number between 1 and 6: "))
def roll_dice(num):
computer_dice = random.randint(1, 6)
if num > computer_dice:
print("Congratulations you win! Your opponent's dice is:", computer_dice)
elif num < computer_dice:
print("Sorry but you lose! Your opponent's dice is:", computer_dice)
else:
print("Draw. Your opponent's dice is:", computer_dice)
roll_dice(num)
Thank you in advance!
I think the problem is that you are trying an empty string to an integer. Same problem when you type in alphabetical characters.
You can use try and except to try the conversion of the input to an integer and then when it failed you run the loop again and when the conversion was successfully you have your number.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
I want this python code comes out from loop when I enter 0 as input number of "num" variable But it continues printing these three lines constantly
Thanks
num = 10 #10 is dummy number for starting loop
while(num!=0):
print("1)Test func1")
print("2)Test func2")
print("0)Exit")
num = input("Enter a number:")
print("Comes out from while loop!")
There reason behind is that the input takes input as a string and you have to either convert it to int:
num = int(input("Enter a number:"))
or change the while loop:
while(num!='0'):
print takes input as string , in order to come out of loop
use
num = int(input("Enter a number:"))
it will consider num variable as an integer but not an string and will come out of the loop.
You have got a correct answer before this but you can change your code too:
while True:
print("1)Test func1")
print("2)Test func2")
print("0)Exit")
num = input("Enter a number:")
if num == '0':
break
print("Comes out from while loop!")
at the begining while always get True, you don't need to set value, 'break' means 'stop current loop and exit'
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 months ago.
I'm writing a simple program that compares two variables
from random import randint
result = (randint(1, 6))
guess = input('Guess the number: ')
if guess == result:
print('You got it right')
else:
print('Wrong')
print(result)
The program sets the variable result as a random number, and then the user inputs a number that they guess. However, even when they get the number right, it says that they were wrong.
How can I get it so that when they get the correct number, it says that they are right?
Thanks in advance!
Either make INTEGER type to STRING or vice versa so that comparison can be done. Two different types (INT and STR) can not be compared.
e.g. code 01 [ both compared variables are string]
from random import randint
result = (randint(1, 6))
guess = input('Guess the number: ')
if guess == str(result):
print('You got it right')
else:
print('Wrong')
print(result)
e.g. code 02 [ both compared variables are integer]
from random import randint
result = (randint(1, 6))
guess = input('Guess the number: ')
if int(guess) == result:
print('You got it right')
else:
print('Wrong')
print(result)
Here you are comparing an int to a string due to return type of input() being string. That will always give False.
Change your input guess to :
guess = int(input())
So,
IN : result = 5
IN : guess = 5
OUT : 'You got it right'
change guess = input('Guess the number: ') to guess=int(input(Guess the number : ')
because input() take a string as a user input.