2012-01-15 47 views
0

我剛剛完成我的家庭作業作家的程序,現在我有一個非常惱人的問題爲什麼我不能在python中重新運行一個函數?

我做到了,所以當一個函數完成它詢問是否要重新運行的主要功能,當我這樣做,那麼運行不同的功能(對不起,如果我吮吸措辭的東西)該功能什麼也沒有。有什麼我可以做的嗎?

這裏是我的代碼

agenda=open("agenda.txt","a") #open the notepad file 
def choice(): #pick the period 
    choice=input("type write, read, or clear\n") 

    if choice=="read": 
     read() 
    elif choice=="write": 
     write() 
    elif choice=="clear": 
     clear() 
    else: 
     print("Invalid Choice") 


def write(): #write the homework 
    per=input("What period is it") 
    hw=input("What is the homework") 
    if per=="1": 
     agenda.write("Period 1:") 
     agenda.write(hw) 
     agenda.write("\n") 
    elif per=="2": 
     agenda.write("Period 2:") 
     agenda.write(hw) 
     agenda.write("\n") 
    elif per=="3": 
     agenda.write("Period 3:") 
     agenda.write(hw) 
     agenda.write("\n") 
    elif per=="4": 
     agenda.write("Period 4:") 
     agenda.write(hw) 
     agenda.write("\n") 
    elif per=="5": 
     agenda.write("Period 5:") 
     agenda.write(hw) 
     agenda.write("\n") 
    elif per=="6": 
     agenda.write("Period 6:") 
     agenda.write(hw) 
     agenda.write("\n") 
    elif per=="7": 
     agenda.write("Period 7:") 
     agenda.write(hw) 
     agenda.write("\n") 
    elif per=="8": 
     agenda.write("Period 8:") 
     agenda.write(hw) 
     agenda.write("\n") 
    else: 
     print("Non existant period") 
    again=input("Would you like to read the homework, clear, or read again? (yes or no)") 
    if again=="yes": 
     choice() 
    elif again=="no": 
     print("\n") 



def clear():#clear the whole thing 
    ajenda = open('agenda.txt', 'r+') 
    ajenda.truncate() 
    again=input("Would you like to read the homework, clear, or read again? (yes or no)") 
    if again=="yes": 
     choice() 
    elif again=="no": 
     print("\n") 

def read():#read the homework 
    read=open("agenda.txt","r") 
    readf=read.read() 
    print(readf) 
    read.close 
    again=input("Would you like to read the homework, clear, or read again? (yes or no)") 
    if again=="yes": 
     choice() 
    elif again=="no": 
     print("\n") 

choice() 
agenda.close() 
+0

程序的輸出以及預期結果可能會有所幫助。 – 2012-01-15 04:12:19

+0

我想你想使用raw_input()而不是input() – 2012-01-15 04:13:18

+2

@VaughnCato:raw_input在python 3中不見了。 – 2012-01-15 08:35:31

回答

2

我跑你的代碼在python2.7,因爲我沒有python3現在。

我的猜測是你跑了你的代碼,寫了一些家庭作業,然後你要求閱讀它,沒有出現。當您寫入文件時,出於性能方面的原因,只有在提供了一定數量的數據或關閉文件之後,緩衝區纔會進入文件。

如果你測試你的代碼然後退出程序,你會發現你的數據在文件中。您可以考慮在write方法中添加flush()調用。

相關問題