[Python] 纯文本查看 复制代码
# 定义函数
def add(x, y):
"""相加"""
return x + y
def subtract(x, y):
"""相减"""
return x - y
def multiply(x, y):
"""相乘"""
return x * y
def divide(x, y):
"""相除"""
return x / y
def opposite(x, y):
"""相反数"""
return - x
def remainder(x,y):
"""求余"""
return x % y
print("选择运算:")
print("1.相加")
print("2.相减")
print("3.相乘")
print("4.相除")
print("5.乘方")
print("6.相反数")
print("7.绝对值")
print("8.取余")
choice = input("输入你的选择(1/2/3/4/5/6/7/8):")
print("请输入进行运算的两个数字(求同一个数相反数和绝对值输入同一数字即可)")
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
elif choice == '5':
print(num1,"的",num2,"次方=", pow(num1,num2))
elif choice == '6':
print(num1,"的",num2,"相反数=", opposite(num1,num2))
elif choice == '7':
print(num1,"的绝对值=",abs(num1))
elif choice == '8':
print(num1,"除以",num2,"的","余数=", remainder(num1,num2))
else:
print("非法输入")
choice2 = input("是否继续运行,选择T或F :")
while choice2 == "T":
def add(x, y):
"""相加"""
return x + y
def subtract(x, y):
"""相减"""
return x - y
def multiply(x, y):
"""相乘"""
return x * y
def divide(x, y):
"""相除"""
return x / y
def opposite(x, y):
"""相反数"""
return - x
def remainder(x,y):
"""求余"""
return x % y
print("选择运算:")
print("1、相加")
print("2、相减")
print("3、相乘")
print("4、相除")
print("5、乘方")
print("6、相反数")
print("7、绝对值")
print("8、取余")
choice = input("输入你的选择(1/2/3/4/5/6/7/8):")
print("请输入进行运算的两个数字(求同一个数相反数和绝对值输入同一数字即可)")
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
elif choice == '5':
print(num1,"的",num2,"次方=", pow(num1,num2))
elif choice == '6':
print(num1,"的",num2,"相反数=", opposite(num1,num2))
elif choice == '7':
print(num1,"的绝对值=",abs(num1))
elif choice == '8':
print(num1,"除以",num2,"的","余数=", remainder(num1,num2))
else:
print("非法输入")
choice2 = input("是否继续循环运行,选择T或F :")
if choice2 == "F":
import sys
sys.exit(0)
else:
print("非法输入")
if choice2 == "F":
import sys
sys.exit(0)
else:
print("非法输入")