首页 > 解决方案 > python中的函数错误

问题描述

我正在研究 python,当我编写一个简单的计算器时,我发现了这个错误。不管我调用什么函数,操作总是加起来。有人可以帮我吗?

编码:

类CalculadoraTerminal:

# Addition

def addition(add, to_add):
    return add + to_add

# Subtraction

def subtraction(sub, to_sub):
    return sub - to_sub

# Multiplication

def multiplication(multi, to_multi):
    return multi * to_multi

# Division

def division(div, to_div):
    return div / to_div

# Menu

menu = input("A - Addition \nS - Subtraction \nM - Multiplication \nD - Division \nSelect you operation:")

if "A" == menu or "a":
    print(addition(int(input("Type the initial Number: ")), int(input("Type the number to add: "))))

if "S" == menu or "s":
    print(subtraction(int(input("Type the initial number: ")), int(input("Type the number to subtract: "))))

if "M" == menu or "m":
    print(multiplication(int(input("Type the initial number: ")), int(input("Type the number to multiply: "))))

if "D" == menu or "d":
    print(division(int(input("Type the initial  number: ")), int(input("Type the number to divide: "))))

错误示例:

A - 加法 S - 减法 M - 乘法 D - 除法 选择您的运算:d 输入初始数字:20 输入要加的数字:2 22

标签: python

解决方案


该行if menu == "A" or "a":可以重写为if menu == "A"OR if 'a'

如果您想检查菜单是“A”还是“a”,请执行

if menu == "A" or menu == "a":

推荐阅读