How to create a python file from given results, I need to save it in a text document or in excel. Any suggestions?
3 Answers
Whether the quiz code is right or not, whether it can be optimized or not, here's a little help on your error. Since Python says that in
text_file.write(score)
there must be a string argument, not an int, just typecast your score variable into a string as in
text_file.write(str(score))
and go from there.
Comments
The error message says everything, your score variable holds an int, but it needs to be a string to be written to a file. (It even tells you the line number.)
So convert it to a string, like this: text_file.write(str(score))
Apart from that, you have a bug in your code caused by a typo in a variable name:socre = score + 0. This does not cause the error, but will cause the code not to work as expected. This is a perfect example of why lazyness can be terrible when programming: By copy-pasting your code block, you copied the bug as well, and now you have multiple bugs by copying a redundant code. Using a function instead and calling it multiple times would have kept the bug in a single place.
Also you are importing the same module random multiple times, which hurts readability of your code a lot. Put all imports on the top of your module, and do not write the same import multiple times (once it is imported it stays imported).
Comments
I think that this page could be a good starting point for you: http://www.python-excel.org/
It points to a number of different modules that should be able to do what you're asking for. (As long as I understood your question properly ;) )
// Mike
csvmodule: docs.python.org/2/library/csv.html