2017-10-19 33 views
0

我需要一個地方來存儲我的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 
+0

代碼的縮進似乎有點關閉,這對於Python代碼可能會導致混淆......爲了解決您的問題,我建議查看使用對象並創建自己的類來管理您的按摩列表。 – Laurent

回答

0

您可以將其輸出到一個單獨的文件然後讓以後的程序看,而是可能把事情搞得一團糟,如果你不小心做一些文件...

+0

提供這樣的例子。 – GiantsLoveDeathMetal

+0

#so將輸出,值等存儲在變量中。 –

+0

然後執行f = open('nameoffile.txt','w')然後f.write(variablename)然後f.close()將存儲的值輸出到名爲nameoffile.txt的外部文件 –

相關問題