我正在研究一個簡單的計算器,它可以帶多個數字。我從我寫的一個更簡單的計算器中獲得了所需的其他代碼。運算符問題和分解數學表達式
這段代碼試圖分解字符串。我在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