python代码生成8道四则运算题目,每道题目的运算符号随机生成,每道题前面有题号,运算数是1~100之间的整数,计算答案保留2位小数。并在s所有题目最后一起给出答案。
时间: 2024-02-19 14:58:39 浏览: 55
好的,以下是Python实现的代码:
```python
import random
# 生成题目
def generate_question():
operators = ['+', '-', '*', '/']
num1 = random.randint(1, 100)
num2 = random.randint(1, 100)
operator = random.choice(operators)
question = f"{num1} {operator} {num2}"
answer = round(eval(question), 2)
return question, answer
# 生成8道题目
questions = []
answers = []
for i in range(1, 9):
question, answer = generate_question()
questions.append(f"{i}. {question} = ")
answers.append(str(answer))
# 输出所有题目和答案
print('\n'.join(questions))
print('答案:' + ', '.join(answers))
```
运行结果如下:
```
1. 7 + 30 =
2. 64 / 95 =
3. 10 * 76 =
4. 12 + 80 =
5. 26 * 50 =
6. 8 - 69 =
7. 10 + 53 =
8. 23 / 18 =
答案:37.0, 0.67, 760.0, 92.0, 1300.0, -61.0, 63.0, 1.28
```
以上代码实现了生成8道随机四则运算题目,每道题目的运算符号随机生成,每道题前面有题号,运算数是1~100之间的整数,计算答案保留2位小数,并在所有题目最后一起给出答案。
阅读全文