Videos
In this example:
x = int(input("x: "))
print(f'x = {x}')if the input is a string, a ValueError is raised. Why not a TypeError? An invalid data type is inputted after all.
When you get a traceback error, look at the last line to see the top level cause for error.
my_file.write(line +'\n')
&
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Clearly it hints to the expression line +'\n'
The +operator expects both arguments to be of same type.(It cant find any overloaded function definition that takes an int and a string.
This is because line is an integer (as generated by randint) while '\n' is a string.
So typecast line to a string
line -> str(line).
The new correct line should be
my_file.write(str(line) +'\n')
As the error message explains it, TypeError: unsupported operand type(s) for +: 'int' and 'str'. You can't concatenate an "integer" (line, which is random.randint(1,500) with a "string" '\n'.
You can do the following:
Copymy_file.write(str(line) +'\n')