python - 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
解决方案
该行if menu == "A" or "a":
可以重写为if menu == "A"
OR if 'a'
。
如果您想检查菜单是“A”还是“a”,请执行
if menu == "A" or menu == "a":
推荐阅读
- java - 如何正确合并两个排序数组?
- .net-core - 如何在批处理文件中运行 .net 核心应用程序
- java - 如果工作流程完成/失败,则更新数据库
- github - 如果 git 分支被删除,为什么可以结帐?
- javascript - 如何使粘性元素“浮动”
- jenkins - groovy.lang.MissingPropertyException:没有这样的属性:类的工件:groovy.lang.Binding
- ruby - FactoryBot 动态属性
- php - SQL - SQL 注入的真正危险是什么?
- javascript - 如何对 Polymer 3.0 中的属性和子属性进行“批量属性更改”
- html - 引导标题在弹回之前折叠一秒钟?