最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Github订阅地址

运维笔记admin1浏览0评论
#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()

发布评论

评论列表(0)

  1. 暂无评论