开发一个循环5次计算小游戏,每次随机产生1-10的数字以及随机选择“+、-、*”运算符
时间: 2023-06-09 21:02:04 浏览: 106
好的,我可以为您编写这个小游戏。以下是 Python 代码实现:
```python
import random
for i in range(5):
num1 = random.randint(1, 10) # 产生随机数1
num2 = random.randint(1, 10) # 产生随机数2
operator = random.choice(["+", "-", "*"]) # 随机选择运算符
# 根据运算符计算结果
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
else:
result = num1 * num2
print("第 %d 次运算:" % (i+1))
print("%d %s %d = ?" % (num1, operator, num2))
# 循环读取用户输入直到输入正确答案为止
while True:
user_ans = input("请输入答案:")
if user_ans.isdigit() and int(user_ans) == result:
print("回答正确!")
break
else:
print("回答错误,请重新输入!")
```
每次运行这段代码,会输出5次随机运算题,用户需要输入答案,直到输入正确答案为止。希望这个小游戏能满足您的需求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)