当然可以,以下是一个简单的计算器程序:

def calculator():
    while True:
        operation = input('请输入运算符号(+,-,*,/): ')  
        if operation not in ['+', '-', '*', '/']:
            print('输入有误,请重新输入。')
            continue
        else:
            break
    num1 = float(input('请输入第一个数字: '))
    num2 = float(input('请输入第二个数字: '))
    if operation == '+':
        result = num1 + num2
    elif operation == '-':
        result = num1 - num2
    elif operation == '*':
        result = num1 * num2
    else:
        result = num1 / num2
    print('计算结果为: ', result)

calculator()

运行结果:

请输入运算符号(+,-,*,/): +
请输入第一个数字: 10
请输入第二个数字: 5
计算结果为:  15.0
Python 简单计算器程序示例

原文地址: https://www.cveoy.top/t/topic/mx6E 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录