我的代碼需要一個常規段落文件並將其切換爲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()
有對'高清code_ROT' ......沒有return語句後'editversion = code_ROT13('使這個沒有。你的誤差應有意義現在 –
我用Google搜索錯誤並在它產生可能的解決方案; http://stackoverflow.com/questions/21689365/python-3-typeerror-must-be-str-not-bytes-with-sys-stdout-write –