程序随机产生一道100以内整数的四则运算题
时间: 2024-05-10 12:20:50 浏览: 69
基于pythonGUI开发小学生四则运算练习软件(附源码).zip
5星 · 资源好评率100%
以下是Python代码实现:
```python
import random
# 随机生成两个数
num1 = random.randint(0, 100)
num2 = random.randint(0, 100)
# 随机生成运算符
operator = random.choice(['+', '-', '*', '/'])
# 判断除数是否为0
if operator == '/' and num2 == 0:
num2 = random.randint(1, 100)
# 计算结果
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
result = num1 / num2
# 打印题目和答案
print(f"{num1} {operator} {num2} = ?")
print(f"答案:{result}")
```
运行结果可能如下:
```
59 * 32 = ?
答案:1888
```
阅读全文