编写二个猜数的小游戏,在程序里设定一个数,运行程序让别人猜。如果猜的教比设定的数大,给出“大了”的提示,如果猜的数比设定的数小,则给出“小了”的提示,设定有5次机会,5次内清中给出“清中”的那示,否则提示“你输了”
时间: 2024-06-01 14:10:24 浏览: 8
# 第一版猜数字游戏
import random
# 生成1-100内的随机数
answer = random.randint(1, 100)
# 初始化猜测次数
guess_times = 0
while guess_times < 5:
guess = int(input("请猜一个1-100内的整数:"))
guess_times += 1
if guess == answer:
print("恭喜你,猜对了!")
break
elif guess < answer:
print("小了")
else:
print("大了")
if guess_times == 5:
print("很遗憾,你没有猜对,正确答案是:", answer)
# 第二版猜数字游戏
import random
# 生成1-100内的随机数
answer = random.randint(1, 100)
# 初始化猜测次数
guess_times = 0
while guess_times < 5:
guess = int(input("请猜一个1-100内的整数:"))
guess_times += 1
if guess == answer:
print("恭喜你,猜对了!")
break
elif guess < answer:
print("小了")
else:
print("大了")
# 根据猜测次数给出提示
if guess_times == 3:
print("提示:正确答案在1-100内")
elif guess_times == 4:
print("提示:正确答案的个位数字是", answer % 10)
if guess_times == 5:
print("很遗憾,你没有猜对,正确答案是:", answer)
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)