2017-03-01 250 views
1

後,我感到有一些問題,我的家庭作業的結構蟒蛇&初學者如何讓我的Python代碼走回到環路;我的任務是:「編寫一個程序,要求用戶輸入文件名,打開文件並讀取文件一次,然後向用戶報告字符數(包括空格和行尾字符),字數,以及文件中的行數拋出異常

如果用戶輸入了一個不存在的文件的名稱,那麼程序應該給她儘可能多的嘗試,以便輸入一個有效的文件名。來自用戶的有效文件名是一種常用操作,因此首先編寫一個單獨的可重複使用的函數,該函數會重複詢問用戶的文件名,直到她鍵入文件中的程序能夠打開。 而且,我也沒有啓動方式(&現在我想知道如果用我的方式已經與它的結構「與/爲」,有一個辦法,甚至這樣做,但現在我的問題越來越它去在拋出錯誤後回到代碼的try部分(我錯過了解釋這個的類,所以我只讀過這個,所以我知道我沒有做正確的事情)。我可以讓它工作很長時間因爲它的存在,如果它不是一個文件名,不打印輸出到屏幕下面是我的代碼:

filename = input("please enter a file name to process:") 



lineCount = 0 
wordCount = 0 
charCount = 0 
try: 

    with open(filename, 'r') as file: 
     for line in file: 
      word = line.split() 
      lineCount = lineCount + 1 
      wordCount = wordCount + len(word) 
      charCount = charCount + len(line) 

    print("the number of lines in your file is:", lineCount) 
    print("the number of words in your file is", wordCount) 
    print("the number of characters in your file is:", charCount) 

except OSError: 

    print("That file doesn't exist") 
    filename = input("please enter a file name to process:") 

而且,我不知道我該怎麼做 - 我是否應該放棄了這個整體思路簡單的嘗試:開放(文件名,「R」)/除外:它的功能= F有反正挽救這個

所以,我認爲解決這個問題是這樣的:

def inputAndRead(): 
"""prompts user for input, reads file & throws exception""" 
filename = None 
    while (filename is None): 
     inputFilename = input("please enter a file name to process") 
     try: 
      filename = inputFilename 
      open(filename, 'r') 
     except OSError: 
      print("That file doesn't exist") 
    return filename 



inputAndRead() 

lineCount = 0 
wordCount = 0 
charCount = 0 

with open(filename, 'r') as file: 
for line in file: 
    word = line.split() 
    lineCount = lineCount + 1 
    wordCount = wordCount + len(word) 
    charCount = charCount + len(line) 

print("the number of lines in your file is:", lineCount) 
print("the number of words in your file is", wordCount) 
print("the number of characters in your file is:", charCount) 

但是,我發現了錯誤:NameError: name 'file' is not defined

+0

您需要一個外部'while'循環,以便在失敗時重複代碼。見http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – roganjosh

+0

可能的複製[如何使迭代中的腳本等到互聯網連接重建?(http://stackoverflow.com/questions/42344303/how-to-make-a-script-wait-within-an-iteration-until-the-internet-connection-is-r) –

+0

爲什麼不使用os.path.exists()來檢查文件是否存在,而不是依賴open所拋出的異常。 –

回答

-1

寫一個無限循環while True。當文件名正確時,在try的末尾添加break

高興的是,幫助

1

,以使文件在循環中打開我會整理這些代碼。無論用戶輸入多少次無效的文件名,代碼都會要求一個新的文件名並重試。

lineCount = 0 
wordCount = 0 
charCount = 0 

f = None 
while f is None: 
    filename = input("please enter a file name to process:") 
    try: 
     f = open(filename) 
    except OSError: 
     print("That file doesn't exist") 

for line in file: 
    word = line.split() 
    lineCount = lineCount + 1 
    wordCount = wordCount + len(word) 
    charCount = charCount + len(line) 

print("the number of lines in your file is:", lineCount) 
print("the number of words in your file is", wordCount) 
print("the number of characters in your file is:", charCount) 
+0

偉大的答案,你可以添加一個'break',這樣用戶就可以退出,但不知道這是否需要OP的作業 – WhatsThePoint

+0

一個普通的'Ctrl + C'會讓用戶退出循環,編碼一些常量的「停止」字符串將是煩惱不得不從用戶的角度來記憶。 –

+0

我想這就是真實的肯定 – WhatsThePoint