The game asks the user to input a number between 1
and 5
and see whether they match the computers random guess.
My code doesn't seem to work when I get to the if
else
statements, and am having trouble figuring it out.
from random import randint
print "I am thinking of a number between 1 and 5";
print "What do you think the number is that I am thinking of",
guess = raw_input("> ")
cguess = (randint(1,5))
print "Your guess was", guess,
print "\nMine was", cguess,
if guess == cguess
print "Well done you win"
if guess != cguess
print "You lose"
import random
guess = int(input('guess a no between 1 to 5 :'))
cguess = random.randint(1,5)
if guess == cguess:
print('Well done you win')
else:
print('You lose')
You're lacking the colon character ( :
) at the end of your if
statements.
if guess == cguess:
print("Well done you win!")
else:
print("You lose")
It's skipping guess == cguess
condition because raw_input()
returns a string
, not an int
. Cast your cguess
variable first ( str(cguess)
).
raw_input returns a string, so you are comparing a string to an integer. you need to turn the string into an integer
guess = int(raw_input("> ")) # <----- see the int call
now guess == cguess will work (if you guessed well)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.