I'm trying to add saves to my code. The loading works, and I am able to write the strings into the text file, but I can't figure out how to write an integer (number) into the txt file.
I've tried to define the integers as strings but none have worked.
(Variable names are in Norwegian.)
def save():
with open("a.txt") as f:
f.write(int(penger))
f.write(int(vognplass_list[0]))
f.write(int(vognplass_list[1]))
f.write(int(vognplass_list[2]))
f.write(str(vognplasser[0]))
f.write(str(vognplasser[1]))
f.write(str(vognplasser[2]))
The error says:
TypeError: write() argument must be str, not int
ints? Based on your explicitint(...)conversion, I'd assume the items are strings, which is the type that must be used forwritealreadyf.writeexpects astrso that it can usestr.encodeto produce the necessary bytes to write to the file, but it doesn't go a step further and try to callstron any non-strarguments first.printfunction will make implicit calls tostr, but that's because it's intended to be a convenient wrapper aroundsys.stdout.write.writeis the "disciplined" function, whose only convenience is making implicit calls toencodewhen the underlying file was opened in text mode.