用python生成2个1--99的整数做加减乘除运算,再与输入的结果做对比,如相等则显示Good!否则显示Try Again!
时间: 2023-05-24 18:03:57 浏览: 145
基于pythonGUI开发小学生四则运算练习软件(附源码).zip
5星 · 资源好评率100%
以下是Python代码实现:
```python
import random
# 生成两个随机整数
num1 = random.randint(1, 99)
num2 = random.randint(1, 99)
# 随机运算符
operator = random.choice(['+', '-', '*', '/'])
# 根据运算符计算正确结果
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
else:
result = num1 / num2
# 输出题目和获取用户输入答案
print(f"{num1} {operator} {num2} = ")
answer = float(input())
# 比较用户输入答案和正确结果
if answer == result:
print("Good!")
else:
print("Try Again!")
```
示例输出:
```
57 * 39 =
2223
Good!
```
阅读全文