# 猜数字游戏
import random, os
#输入值的函数,且防止输入非数字
def input_num():
while True:
try:
num = int(input('\nPls enter an integer which you think is the right num: '))
return num
break
except ValueError:
print('Pls enter an integer!')
#取随机数
rn = random.randint(0 ,1000)
low, high = 0, 1000
print('************Guess A Num Betweem 0 And 1000!************\n')
while True:
#调用输入函数
num = input_num()
#清空界面,告知使用者结果正确
if num == rn:
os.system('cls')
print('You enter the right num! It is:', num)
break
#提示输入的值不在范围内
elif num < low or num > high:
print('Pls enter a num between', str(low), 'and', str(high))
#输入的值大于随机数,重新给定范围
elif num > rn:
print('It is higher')
high = num
print('Pls enter a num between', str(low), 'and', str(high))
#输入的值小于随机数,重新给定范围
elif num < rn:
print('It is lower')
low = num
print('Pls enter a num between', str(low), 'and', str(high))
Python3.x | 猜数字游戏
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 正文之前 没错,我就是这么不学无术,C++实在学的鸡儿疼,所以干脆搞点娱乐措施,昨天赶上了京东图书做大活动,所以屯...
- Centos中默认安装的是Python2.x但是现在很多人学习的都是Python3.x版本,所以升级Pytho...
- 先介绍一下 python bytes和str两种类型转换的函数encode(),decode() str通过enc...