Python初学者(猜猜随机数游戏)

时间:2016-03-15 05:28:50

标签: python

游戏要求用户在15之间输入一个数字,看看它们是否与计算机随机猜测匹配。

当我到达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"

3 个答案:

答案 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会起作用(如果你猜对了)