1

My code takes a regular paragraph file and switches it up to ROT13 characters.
I keep getting the error ("TypeError: write() argument must be str, not None") and have no idea why/what it's talking about.
My code works fine outputting everything correctly into the file, but this error is really bugging me.

Function

# 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()

Main

# 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()
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 2
    There's no return statement for `def code_ROT`... `editversion = code_ROT13(` makes this None. Your error should make sense now – OneCricketeer Dec 07 '16 at 03:10
  • 2
    I googled for the error and it yield possible solution at; http://stackoverflow.com/questions/21689365/python-3-typeerror-must-be-str-not-bytes-with-sys-stdout-write – Umar Yusuf Dec 07 '16 at 03:14

1 Answers1

0

A function with no return, returns None. You've assigned that to editversion, which you then attempt to write to a file.

However, your method accepts the file as a parameter and is already writing to it.

Just call this. Remove the equals before it and the line after it

code_ROT13(file_variable_input, file_variable_output)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • When I remove the "editversion line" and the one below I get the error ("AttributeError: 'str' object has no attribute 'close'") – IncognitoBatman Dec 07 '16 at 03:19
  • Yes. You closed the string, not the file... Read the error. Think about what it says. `file_variable_input`? Is that what you want to close? That's not what your code does – OneCricketeer Dec 07 '16 at 03:24
  • Ah i see what u mean, I closed the name of it instead of the actual file. Silly mistakes were made here rip. Thanks for your help though :D – IncognitoBatman Dec 07 '16 at 03:28
  • Also, note, your code overwrites `input("Enter input file name: ")`, for example, so you'll want to fix that maybe – OneCricketeer Dec 07 '16 at 03:31