You don't need to include the newline character in your str() call. You use str() to convert a non-string to a string, and then you can concatenate your newline character to that string:
You are going to encounter a few more errors due to missing parentheses, but you should be able to figure those out when Python alerts you to them. Also, be aware that input() always returns a string value. That means the user's input will not equal the integer value of 0 when you check it on line 30, even if they typed in '0'. You need to account for that by either converting the input or changing your equals comparison.
f.write(str(a1) + '\n')However, you aren't going to get the results you want in your file. Every time you open a file with 'w' as the mode, you are overwriting any contents that already exist (essentially erasing the existing file). If you want to add new content without erasing what is already there, you need to open with 'a' (for "append"). You might find it more efficient to just open and close each file once total instead of once per question.
You are going to encounter a few more errors due to missing parentheses, but you should be able to figure those out when Python alerts you to them. Also, be aware that input() always returns a string value. That means the user's input will not equal the integer value of 0 when you check it on line 30, even if they typed in '0'. You need to account for that by either converting the input or changing your equals comparison.