我需要一個地方來存儲我的addmassage()函數的返回數據,稍後會被引用。我想在沒有全局變量的情況下做到這一點,但這有可能嗎?這是我的一些代碼。如何存儲函數的返回值並將它們加在一起
def mainFunction():
massageList = [0, 0, 0]
"""This function hosts the menu and """
print "Make a selection:"
print "1. Add a massage to my schedule"
print "2. View my current schedule"
print "3. Cancel a massage from my schedule"
print "4. Calculate fundraising total"
print "5. Quit"
selection = raw_input("Your selection:")
if selection == "1":
addMassage1 = addMassage()
massageList[0] = massageList[0] + addMassage1[0]
massageList[1] = massageList[1] + addMassage1[1]
massageList[2] = massageList[2] + addMassage1[2]
print massageList
mainFunction()
if selection == "2":
print massageList
mainFunction()
def addMassage():
massageList = [0, 0, 0]
massageType = raw_input("Add a (1) 15 minute, (2) 30 minute, or (3) 60 minute massage")
if massageType == "1":
massageList[0] = massageList[0] + 1
if massageType == "2":
massageList[1] = massageList[1] + 1
if massageType == "3":
massageList[2] = massageList[2] + 1
return massageList
enter code here
代碼的縮進似乎有點關閉,這對於Python代碼可能會導致混淆......爲了解決您的問題,我建議查看使用對象並創建自己的類來管理您的按摩列表。 – Laurent