如何在 Python 3 中制作计算器

How to make a calculator in Python 3

我需要在 Python 中创建一个可以执行所有这些任务的计算器。我已经用我的代码进行了加法、减法、乘法和除法。有人可以告诉我要在我的代码中添加什么来为我的计算器添加对数、幂、勾股定理和阶乘吗?以下是我的计算器的所有要求。

每个数学运算都需要独立function/method: 加法 - 最多接受 5 个参数, 减法 - - 接受 3 个参数, 乘法 - 接受 4 个参数, 除法 - 接受 2 个参数, 日志 - 采用 1 个参数, 将一个数字提高到一个幂 ex。 X平方, 求解勾股定理, 阶乘

def calculate():
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')

    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)

    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)

    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)

    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)

    else:
        print('You have not typed a valid operator, please run the program again.')

    # Add again() function to calculate() function
    again()

def again():
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        calculate()
    elif calc_again.upper() == 'N':
        print('See you later.')
    else:
        again()

calculate()

这只是您要求的功能的想法。我将其中一些添加到你的计算器中。您还可以更改要向用户显示的提示。

PS:最好将 while 用于计算器循环。

import math

def calculate():
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
! for factorial
log for logarithm(number, base)
hypot for Pythagorean
''')

    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)

    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)

    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)

    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)

    elif operation == '!':
        print(math.factorial(number_1))
        print(math.factorial(number_2))

    elif operation == 'log':
        print(math.log(number_1, number_2))

    elif operation == 'hypot':
        print(math.hypot(number_1, number_2))

    else:
        print('You have not typed a valid operator, please run the program again.')

    # Add again() function to calculate() function
    again()

def again():
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        calculate()
    elif calc_again.upper() == 'N':
        print('See you later.')
    else:
        again()

calculate()

我根据评论更新了答案;你想从用户那里获取可变数量的输入,比如 n 数字。我根据您的情况使用 add() 来回答您的确切问题并删除了其他代码。

def add(numbers_list):
    return sum(numbers_list)

if __name__ == "__main__":
    while(True):
        number_of_inputs = int(input('Number of inputs? '))
        numbers = [float(input(f'Please enter a number({i + 1}): ')) for i in range(number_of_inputs)]
        print(add(numbers))