我正在研究一个简单的计算器,它可以带多个数字。我从我写的一个更简单的计算器中获得了所需的其他代码。运算符问题和分解数学表达式
这段代码试图分解字符串。我在oper_lib
变量变量 中放置运算符时遇到问题此外,我可以使用泛型变量定义函数,并在需要使用它的任何东西上调用相同的函数?
>""" basic calc - designed to take multiple >variables """
from datetime import *
now = datetime.now()
#Intro
print ("\n")
print ("Welcome to BasicCalc:Unstable! \n")
print ("If you need HELP, type help \n")
print (now)
#Beginning processing intake
ui1 = input("Please enter figure: ")
intake_list = ui1.split(" ")
lenth_list= len(intake_list)
if lenth_list % 2 == 0:
print ("invalid entry")
else:
print ("")
"""
Thoughts on this/ ideas:
- build a secondary math op list
- add two for - in loops in quick succession
"""
def do_math(intake_list):
""" proforms math function from a list"""
oper_lib = [
"+" ,
"-" ,
"*" ,
"/"
]
for i in intake_list:
for n in i:
if n in oper_lib:
intake_list.insert(i-1 , " ")
intake_list.insert(i+1 , " ")
print(intake_list)
print (do_math(intake_list))
print (intake_list)
print (lenth_list)
该程序请求一个数字。这个数字是一个数字,一个表达式,是什么?我输入了22,然后用不同的方式输出这三个结果:无['22'] 1.你可以给出你期望输出结果的提示吗? –
这个想法是你输入一个完整的表达式,每个整数/操作数之间有一个空格。该程序将其作为一个字符串,将其分解成一个列表并处理furthor并返回一个答案。所以如果你尝试用5-5来运行程序,> do_math应该把它分解为> ['5',' - ','5'] – user43850
我认为你是自相矛盾的。它看起来像你试图让它'['5','','','','5']'。此外,'do_math(...)'将始终返回None,因为除非您指定具有相应值的'return'关键字,否则函数将返回None。 – BLaZuRE