2016-12-07 120 views
0

我的代碼需要一個常規段落文件並將其切換爲ROT13字符。
我不斷收到錯誤(「TypeError:write()參數必須是str,而不是None」),並且不知道爲什麼/它在說什麼。
我的代碼工作正常,輸出的一切正確的文件,但這個錯誤真的讓我煩惱。TypeError幫助:write()參數必須是str,而不是無

功能

# Constants 
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
ROT13_ALPHABET = "NOPQRSTUVWXYZABCDEFGHIJKLM" 


def code_ROT13(file_variable_input, file_variable_output): 
    line = file_variable_input.readline().strip() 
    alphabet_list = list(ALPHABET) 
    rot13_list = list(ROT13_ALPHABET) 

    while line != "": 
     for letter in line: 
      rot13_edit = False 
      for i in range(len(alphabet_list)): 
       if letter == alphabet_list[i] and not rot13_edit: 
        letter = rot13_list[i] 
        rot13_edit = True 
       elif letter == alphabet_list[i].lower() and not rot13_edit: 
        letter = rot13_list[i].lower() 
        rot13_edit = True 
      file_variable_output.write(letter) 

     line = file_variable_input.readline() 

主要

# Imports 
from cipher import code_ROT13 
# Inputs 
file_name_input = input("Enter input file name: ") 
file_name_input = "code.txt" 
file_variable_input = open(file_name_input, "r") 

file_name_output = input("Enter output file name: ") 
file_name_output = "code_ROT13.txt" 
file_variable_output = open(file_name_output, "w") 

# Outputs 
editversion = code_ROT13(file_variable_input, file_variable_output) 
file_variable_output.write(editversion) 

file_name_input.close() 
+2

有對'高清code_ROT' ......沒有return語句後'editversion = code_ROT13('使這個沒有。你的誤差應有意義現在 –

+2

我用Google搜索錯誤並在它產生可能的解決方案; http://stackoverflow.com/questions/21689365/python-3-typeerror-must-be-str-not-bytes-with-sys-stdout-write –

回答

0

沒有返回一個函數,返回None。您已將其分配給editversion,然後嘗試寫入文件。

但是,您的方法接受該文件作爲參數,並且已經寫入該文件。

只需調用它即可。刪除之前的平等和線它

code_ROT13(file_variable_input, file_variable_output) 
+0

當我刪除「editversion行「和下面的一個我得到的錯誤(」AttributeError:'str'對象沒有屬性'關閉'「) – IncognitoBatman

+0

是的,你關閉了字符串,而不是文件...閱讀錯誤,想想它說什麼。 file_variable_input'?那是你想要關閉嗎?這不是你的代碼 –

+0

啊我明白你的意思,我關閉了它的名字,而不是實際的文件。這裏犯了愚蠢的錯誤。感謝您的幫助:D – IncognitoBatman

相關問題