算术运算测试:功能要求:该程序实现十道100 以内加减法数学题,能根据题目计算
时间: 2023-05-28 20:01:41 浏览: 175
并判断答案是否正确,最后输出做对的题目数量和得分情况。
实现思路:
1.生成随机的加减法数学题,随机数范围是100以内。
2.通过输入获取用户计算的答案。
3.对用户输入的答案进行判断,如果答案正确,则计数器加一,并计算得分。
4.最后输出成功做对的题目数量和得分情况。
代码如下:
```
import random
def test():
count = 0 # 计数器,用来记录做对的题目数量
score = 0 # 得分情况,每做对一题得分10分
for i in range(10): # 循环十次,生成十道数学题
a = random.randint(0, 100)
b = random.randint(0, 100)
op = random.choice(["+", "-"]) # 随机生成加减法符号
if op == "+":
ans = a + b
else:
ans = a - b
print(f"{i + 1}. {a} {op} {b} = ", end="")
try:
user_ans = int(input().strip()) # 获取用户输入的答案,并将其转换为整数
except ValueError:
print("请输入数字!")
continue
if user_ans == ans: # 判断用户输入的答案是否正确
count += 1
score += 10
print("恭喜你,答对了!")
else:
print(f"很遗憾,正确答案是:{ans}")
print(f"\n你一共做对了{count}道题目,得分为{score}分。")
test() # 调用函数运行程序
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)