1.需要引入random模块,引用函数randint(),这个函数返回一个随记的整数
2.用and逻辑运算符比较
代码如下:
import random
secret = random.randint(1,10)
count = 1
temp = input("猜数字:")
guess = int(temp)
while guess != secret and count < 3:
if guess > secret:
print("too big")
else:
print("too small")
temp = input("try again:")
guess = int(temp)
count +=1
if guess ==secret:
print("对了")
print("game over")
print("游戏结束")
原文:http://www.cnblogs.com/MonkeyTesting/p/7643371.html