游戏要求用户在1
和5
之间输入一个数字,看看它们是否与计算机随机猜测匹配。
当我到达if
else
语句时,我的代码似乎无法正常工作,并且无法解决问题。
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"
答案 0 :(得分:0)
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')
答案 1 :(得分:-1)
您在:
声明的末尾缺少冒号(if
)。
if guess == cguess:
print("Well done you win!")
else:
print("You lose")
它正在跳过guess == cguess
条件,因为raw_input()
会返回string
,而不是int
。首先展开cguess
变量(str(cguess)
)。
答案 2 :(得分:-1)
raw_input返回一个字符串,因此您将字符串与整数进行比较。 你需要把字符串变成一个整数
guess = int(raw_input("> ")) # <----- see the int call
现在猜= = cguess会起作用(如果你猜对了)