编写一个100以内的整数加减乘除练习器
时间: 2024-01-21 08:15:51 浏览: 8
好的,以下是一个简单的100以内的整数加减乘除练习器的Python代码:
```python
import random
# 生成随机数
def generate_number():
return random.randint(1, 100)
# 加法练习
def addition():
num1 = generate_number()
num2 = generate_number()
answer = num1 + num2
user_answer = int(input(f"{num1} + {num2} = "))
if user_answer == answer:
print("回答正确!")
else:
print(f"回答错误! 正确答案为{answer}")
# 减法练习
def subtraction():
num1 = generate_number()
num2 = generate_number()
answer = num1 - num2
user_answer = int(input(f"{num1} - {num2} = "))
if user_answer == answer:
print("回答正确!")
else:
print(f"回答错误! 正确答案为{answer}")
# 乘法练习
def multiplication():
num1 = generate_number()
num2 = generate_number()
answer = num1 * num2
user_answer = int(input(f"{num1} * {num2} = "))
if user_answer == answer:
print("回答正确!")
else:
print(f"回答错误! 正确答案为{answer}")
# 除法练习
def division():
num1 = generate_number()
num2 = generate_number()
# 确保num2不为0
while num2 == 0:
num2 = generate_number()
# 计算答案,保留两位小数
answer = round(num1 / num2, 2)
user_answer = float(input(f"{num1} / {num2} = "))
if user_answer == answer:
print("回答正确!")
else:
print(f"回答错误! 正确答案为{answer}")
# 主程序
print("欢迎使用100以内的整数加减乘除练习器!")
print("请选择练习类型:")
print("1. 加法")
print("2. 减法")
print("3. 乘法")
print("4. 除法")
choice = int(input("请输入您的选择: "))
if choice == 1:
addition()
elif choice == 2:
subtraction()
elif choice == 3:
multiplication()
elif choice == 4:
division()
else:
print("无效的选择!")
```
运行程序后,用户可以选择四种不同的练习类型:加法、减法、乘法和除法。程序会随机生成两个数字,并计算正确答案。用户需要输入自己的答案,程序会根据用户的输入给出相应的评价。
阅读全文