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
Related
import time
import random
# USER CHOOSES TO PLAY
play = input("Type 'Play' to play")
while play == "play":
# COMPUTER GENERATES NUMBER
number = random.randint(1,11)
# USER GOES TO GUESS THE NUMBER
print ("guess the number, the computer has picked a number between 1 and 10. You have 5 tries")
num_guess = int(input())
# CODE TELLS USER IF THEY CHOOSES THE RIGHT OR WRONG NUMBERPL
if num_guess == number:
print(f"Yes the number is {number}, good job!")
elif num_guess != number:
for numguesses in range (4, 0, -1):
num_guess = int(input(f"guess the number you have {numguesses} tries"))
play = input("Type 'Play' to play")
i have no idea whether when the computer generates a number with 'random.randint' if it is generating a new number every time i guess what number it has generated (as this is a number generating game). Please can you help and let me know.
Im trying to make it so there is only 1 number generated, from 1,11, and i have 5 guess to guess the right number, then it repeats if the user types 'play'
The first comment correctly answers the question.
the code generates a new random number every time the line random.randint(1,11) is encountered.
not only this, but you can easily prove this to yourself by adding the following to the next line:
print('the number has been generated:', number)
which will print the new number to the console each time...
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
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 3 years ago.
I've been working on a small project to help my coding, and have run into a simple problem, So I'm trying to recreate a small game calls 'odds on' as per where I live, where you and a friend say a number, let's say between 1 and 5, at the same time, and if your friend says the same number as you, you win, basically, and I've got my code working, mostly, however it only says that I lose, even if the computer answer is equal to my answer, have a look at the whole code:
oddsonplayer = input("")
print(oddsonplayer)
import random
oddsoncomputer = (random.randint(1,5))
if oddsonplayer > "5":
print("Pick a number between 1 and 5!")
if oddsonplayer == oddsoncomputer:
print("You Win!")
else:
print("You Lose!")
So long story short, it says I lose, even if both numbers are equal.
Your code is correct. Just that you have a string, not a number. Convert the string to a number like this int(oddsonplayer)
oddsonplayer = input("")
print(oddsonplayer)
import random
oddsoncomputer = (random.randint(1,5))
if oddsonplayer > "5":
print("Pick a number between 1 and 5!")
if int(oddsonplayer) == oddsoncomputer:
print("You Win!")
else:
print("You Lose!")
I created a Mastermind code and when I attempt to run it on my Mac it just says "logout". If anyone knows why, that would be extremely helpful! Here is the code:
def masterMind():
number = random.ranint(10000,99999) #the computer chooses 5 random numbers
userGuess = raw_input("Guess my 5 digit password:") #asking the user to input their guess
tries = 10 # telling the computer number of tries the user is allowed
while tries < 0: # 10 attempts is the maximum amount
if number != userGuess: # if the computer's password does not equal the user's guess then it equals to one attempt
tries += 1
userGuess = input("Guess my 5 digit password:")
else: #if the user and the computer's answers align
print "Win: ", userGuess
print number
tries = 10
while tries < 0:
will never enter the loop.
You may want to reverse the sense of the comparison, using > instead.
You'll also need to decrement tries within the loop rather than incrementing it.
more "pythonic" (2.x) way. fixes answers like "00000", fixes int to str compair.
def masterMind():
number = "%05d" % random.randint(0, 99999) #the computer chooses 5 random numbers
for tries in range(10):
user_guess = raw_input("Guess my 5 digit password:") #asking the user to input their guess
if number == user_guess:
print "Win on the %d try" % (tries + 1)
break
print "answer was:", number