#chatgpt访问和订阅地址邀请码
https://tg4.9999922.xyz/#/register?code=CcXSbB7R
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):
if y == 0:
return "Error! Division by zero."
return x / y
def calculator():
print("欢迎使用简单计算器")
print("选择操作:")
print("1. 加法")
print("2. 减法")
print("3. 乘法")
print("4. 除法")
while True:
choice = input("请输入选择(1/2/3/4),或输入 'q' 退出:")
if choice == 'q':
print("退出计算器")
break
if choice in ['1', '2', '3', '4']:
try:
num1 = float(input("请输入第一个数字:"))
num2 = float(input("请输入第二个数字:"))
if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
except ValueError:
print("无效的输入,请输入有效的数字。")
else:
print("无效的选择,请输入 1、2、3、4 或 'q' 退出。")
if __name__ == "__main__":
calculator()
#chatgpt访问和订阅地址邀请码
https://tg4.9999922.xyz/#/register?code=CcXSbB7R
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):
if y == 0:
return "Error! Division by zero."
return x / y
def calculator():
print("欢迎使用简单计算器")
print("选择操作:")
print("1. 加法")
print("2. 减法")
print("3. 乘法")
print("4. 除法")
while True:
choice = input("请输入选择(1/2/3/4),或输入 'q' 退出:")
if choice == 'q':
print("退出计算器")
break
if choice in ['1', '2', '3', '4']:
try:
num1 = float(input("请输入第一个数字:"))
num2 = float(input("请输入第二个数字:"))
if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
except ValueError:
print("无效的输入,请输入有效的数字。")
else:
print("无效的选择,请输入 1、2、3、4 或 'q' 退出。")
if __name__ == "__main__":
calculator()