python的一个小游戏代码
时间: 2024-06-17 18:07:03 浏览: 14
好的,以下是一个猜数字游戏的 Python 代码示例:
```
import random
def guess_number():
number = random.randint(1, 100)
guess = None
attempts = 0
print("我想了一个数字在1到100之间,你来猜猜看吧!")
while guess != number:
guess = int(input("你猜的数字是多少?"))
attempts += 1
if guess < number:
print("不对哦,再大一点!")
elif guess > number:
print("不对哦,再小一点!")
else:
print("恭喜你,猜中啦!你用了 %d 次机会猜对了数字 %d。" % (attempts, number))
if __name__ == '__main__':
guess_number()
```
运行这个程序后,它会随机生成一个 1 到 100 之间的数字,然后提示你猜这个数字是多少。你可以输入一个数字进行猜测,程序会告诉你猜的数字是太大了还是太小了,直到你猜中为止。最后程序会告诉你你用了多少次机会猜对了数字。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)