本文实例为大家分享了python编写猜数字小游戏的具体代码,供大家参考,具体内容如下
import random secret = random.randint(1,30) guess = 0 tries = 0 print("我叫丁丁,我有一个秘密数字!") print("数字从1到30,你只有6次机会!") while int(guess) != secret and tries < 6: print("你猜的数字是?") guess = input() if int(guess) < secret: print("数字太小了!再猜!") elif int(guess) > secret: print("数字太大了!再猜!") tries = tries + 1 if int(guess) == secret: print("你真厉害!恭喜你猜对啦!") else: print("你猜错6次啦,下次再玩吧!") print("我的秘密数字是:",secret)