主要知识点:
1)函数
一、菜单函数:
程序流程图:
菜单显示代码:
#显示菜单
def Menu():
print("1 加法运算")
print("2 减法运算")
print("3 乘法运算")
print("4 除法运算")
print("0 退出")
choice = int(input("请输入->"))
return choice
二、实现加法程序
流程图:
代码:
def Add():
for each in range(1,M+1):
a = int(random.randint(1,101))
b = int(random.randint(1,101))
ret = a + b
print(each,'->',a,"+",b,"=",end="")
ans = int(input())
if ans == int(ret):
print("回答正确")
else:
print("回答错误,正确答案是->",ret)
完整程序流程图:
完整代码:
#小学生四则运算练习系统
import random #随机数模块
M = 10
#显示菜单
def Menu():
print("1 加法运算")
print("2 减法运算")
print("3 乘法运算")
print("4 除法运算")
print("0 退出")
choice = int(input("请输入->"))
return choice
#加法练习
def Add():
for each in range(1,M+1):
a = int(random.randint(1,101))
b = int(random.randint(1,101))
ret = a + b
print(each,'->',a,"+",b,"=",end="")
ans = int(input())
if ans == int(ret):
print("回答正确")
else:
print("回答错误,正确答案是->",ret)
#减法练习
def Minus():
for each in range(1,M+1):
a = int(random.randint(1,101))
b = int(random.randint(1,101))
if a >= b:
print(each,'->',a,"-",b,"=",end="")
ret = a - b
else:
print(each,'->',b,"-",a,"=",end="")
ret = b - a
ans = int(input())
if ans == int(ret):
print("回答正确")
else:
print("回答错误,正确答案是->",ret)
#乘法练习
def Multip():
for each in range(1,M+1):
a = int(random.randint(1,99))
b = int(random.randint(1,99))
ret = a * b
print(each,'->',a,"✖",b,"=",end="")
ans = int(input())
if ans == int(ret):
print("回答正确")
else:
print("回答错误,正确答案是->",ret)
#除法练习
def Div():
for each in range(1,M+1):
a = int(random.randint(1,99))
b = int(random.randint(1,99))
ret = (a * b )/ b
print(each,'->',a*b,"÷️",b,"=",end="")
ans = int(input())
if ans == int(ret):
print("回答正确")
else:
print("回答错误,正确答案是->",int(ret))
#主函数实现循环
while 1:
choice = Menu()
if choice == 1:
Add()
elif choice == 2:
Minus()
elif choice == 3:
Multip()
elif choice == 4:
Div()
elif choice == 0:
break
else:
print("输入无效")
提示:希望能给您带来帮助。若有问题,希望您能提出来。在学编程的路上一同进步。