加减乘除求余
- 编写菜单模块,实现功能导航.
- 编写所提示所需的函数
- 编写加减乘除求余平方函数
- 主程序调用,用户输入,进行判断
def main():
print('菜单栏:')
print('1.相加 2.相减 3.相乘')
print('4.相除 5.求余 6.平方 0.退出')
def z(choice):
a = ''
if(choice == '1'):
a = '你选择的是加法'
elif(choice == '2'):
a = '你选择的是减法'
elif(choice == '3'):
a = '你选择的是乘法'
elif(choice == '4'):
a = '你选择的是除法'
elif(choice == '5'):
a = '你选择的是求余'
elif(choice == '6'):
a = '你选择的是平方'
elif(choice == '0'):
a = '欢迎下次使用~~~'
else:
a = '输入的有误'
return a
def add(a, b):
return a + b
def jian(a, b):
return a - b
def cheng(a, b):
return a * b
def chu(a, b):
return a / b
def yu(a, b):
return a % b
def pingfang(a, b):
return pow(a,b)
main()
while True:
choice = input("请选择加减乘除:")
print(z(choice))
if(choice == '0'):
break
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", jian(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", cheng(num1, num2))
elif choice == '4':
if num2 != 0:
print(num1, "/", num2, "=", chu(num1, num2))
else:
print("0可以当作被除数,但不能当作除数!")
elif choice == '5':
print(num1, "%", num2, "=", yu(num1, num2))
elif choice == '6':
print(num1, "的", num2, "次方=", pingfang(num1, num2))
else:
print("请输出正确的数字~~~")
print()
输出结果如下所示: