python 编写一个程序,实现100以内的随机四则运算出题,将题目书橱在屏幕上,然后接收拥护输入,判断回答是否正确,将结果输出到屏幕上,如果回答正确,则提示回答正确,如果回答错误,在提示回答错误的同时将正确答案输出
时间: 2023-05-21 22:05:16 浏览: 165
以下是 Python 代码:
```python
import random
def generate_question():
a = random.randint(0, 100)
b = random.randint(0, 100)
op = random.choice(["+", "-", "*", "/"])
if op == "+":
answer = a + b
elif op == "-":
answer = a - b
elif op == "*":
answer = a * b
else:
answer = a // b
return f"{a} {op} {b} = ", answer
if __name__ == "__main__":
print("欢迎来到四则运算游戏!")
correct = 0
total = 0
while True:
question, answer = generate_question()
print(question)
try:
user_answer = int(input("请输入答案:"))
if user_answer == answer:
print("回答正确!")
correct += 1
else:
print(f"回答错误!正确答案是{answer}")
except ValueError:
print("请输出数字!")
total += 1
print(f"你已经回答了{total}题,其中{correct}题回答正确。")
```
这个程序可以生成随机的四则运算题目,接受用户输入,并判断输入是否正确。程序将持续运行直到用户主动停止。
阅读全文