一个简单的猜大小的小游戏 python

时间:2021-05-25 09:13:08

初学python,用python写了一个简单的猜大小的小游戏

#!/usr/bin/env python
#-*- coding:utf-8 -*-
print "--------------测试--------------" temp = input("猜一下我心里想的是那个数 ")
guess = int(temp) while guess != 8:
temp = input("哎呀,猜错了,请重新输入吧:")
guess = int(temp)
if guess == 8:
print "猜对了诶"
print "哼,猜对了也没有奖励"
else:
if guess > 8:
print "哥,大了大了"
else:
print "嘿,小了小了"
print "游戏结束,不玩了,嘿嘿"

使用random模块,产生一个随机答案

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import random
secret = random.randint(1,10)
print "--------------测试--------------" temp = input("猜一下我心里想的是那个数 ")
guess = int(temp) while guess != secret:
temp = input("哎呀,猜错了,请重新输入吧:")
guess = int(temp)
if guess == secret:
print "猜对了诶"
print "哼,猜对了也没有奖励"
else:
if guess > secret:
print "哥,大了大了"
else:
print "嘿,小了小了"
print "游戏结束,不玩了,嘿嘿"
~