python实现猜数字小游戏
Python初学者小游戏:猜数字
游戏逻辑:电脑随机生成一个数字,然后玩家猜数字,电脑提示猜的数字大了还是小了,供玩家缩小数字范围,达到既定次数后,玩家失败。若在次数内猜对,玩家获胜。
涉及知识点:random.randint(),print(),input()(raw_input())
参考实现代码:
#!/usr/bin/envpython
#encoding:utf-8
#使用print("",end=...)标准
from__future__importprint_function
importos
importsys
importtime
importrandom
#输入检测
while1:
os.system('cls')
print("Hello,WelcometoGuess_NumberGames...TheNumberisbetween1-10...")
print("Pleaseinputthelevelyouwant(1~10):",end='')
level=raw_input("")
diff=11-int(level)
ifdiff>10ordiff<1:
print("InvalidInput...")
time.sleep(0.3)
else:
break
#猜数字流程
count_num=0
ran=random.randint(1,10)
whilecount_numran:
print("TooBig...")
continue
else:
print("Congraduation!YouWin...")
break
ifcount_num==diff:
print("YouLose...")
更多关于python游戏的精彩文章请点击查看以下专题:
python俄罗斯方块游戏集合
python经典小游戏汇总
python微信跳一跳游戏集合
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。