Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
invalid syntax (BEGINNER)
#1
Hi, im a beginner to python and i was trying to test my knowledge by creating a simple game, it's a quiz game of four questions, each question answered correctly gets you five points and logs them into a file called "lista risultati.txt" and when you complete the game your final score is registered on "risultato finale.txt", at the start of the game it asks you if you want to play you need to answer "yes" or "no" if you say no it will exit if you say yes you will play and if you say something else there will be an error and the program will bring you back to the question, i cant manage to find the error in my code that makes the compiler say "invalid syntax", im sorry for bad grammar/spelling english is not my first language, i will report the code of my game and the error below:
Ps. im using python 3.6

CODE:
import sys

print("**********************")
print("*welcome to QuizWorld*")
print("**********************")
print("       v.1.0.0        ")

print("rules: Do not cheat each answer will give you 5 points, if you get more then 15 points you win if you get less you loose, after you win or lose go check out your answers and final points on lista risultati or punteggio finale")
def yes():
	a = input("do you want to play? ")
	if a == "yes":
		del a
		print("ok, so first question")
		print("whats the capital of italy?")
		a = input("answer: ")
		if a == "rome":
			print("correct")
			a1 = 5
			with open("lista risultati.txt", 'w') as f
				f.write(str(a1\n))
		else:
			a1 = 0
			print("wrong")
			with open("lista risultati.txt", 'w') as f
				f.write(str(a1\n))
		f.close
		print("second question")
		print("1*2*3*4*5*6*7*8*9*0*1*1*0*1")
		b = input("answer: ")
		if b == 0:
			print("correct")
			b1 = 5
			with open("lista risultati.txt", 'w') as f
				f.write(str(b1\n)
		else:
			b1 = 0
			print("wrong")
			with open("lista risultati.txt", 'w') as f
				f.write(str(b1\n)
		f.close
		print("third question")
		print("does it weight more 1 pound of steel or 1 pound of feathers")
		c = input("answer: ")
		if c == "they weight the same":
			print("correct")
			c1 = 5
			with open("lista risultati.txt", 'w') as f
				f.write(str(c1\n))
		else:
			c1 = 0
			print("wrong")
			with open("lista risultati.txt", 'w') as f)
				f.write(str(c1\n)
		f.close
		print("FINAL question")
		print("is this program good?")
		d = input("answer: ")
		if d == "yes":
			print("correct")
			d1 = 5
			with open("lista risultati.txt", 'w') as f
				f.write(str(d1))
		else:
			d1 = 0
			print("wrong")
			with open("lista risultati.txt", 'w') as f
				f.write(str(d1))
		f.close
		
		e = a1 + b1 + c1 + d1
		
		with open("risultato finale.txt", 'w') as f1
		f1.write(str(e)
		f1.close
		
		r = open("risultato finale.txt", "r")
		
		if r == 15:
			print("testing results...")
			print("BRAVO, you have winned the game")
		else:
			print("testing results...")
			print("you have lost the game")
		
		
			

	elif a == "no":
		sys.exit("you said to do it")
	else:
		print("!!ERROR: you need to enter yes or no. !!")
		yes()
yes()
ERROR:
Error:
File "C:\Users\Shishi\Desktop\QuizWorld\risultati\game.py", line 21 with open("lista risultati.txt", 'w') as f ^ SyntaxError: invalid syntax
Reply
#2
Your "with open" lines should end with a colon, like in this example:

with open("lista risultati.txt", 'w') as f:
Reply
#3
missing colon at the end of all lines that are with context manager
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Done, but now it prints me out a new error:

Error:
File "C:\Users\ShiShi\Desktop\QuizWorld\risultati\game.py", line 21 with open("lista risultati.txt", 'w') as f ^ SyntaxError: invalid syntax
Reply
#5
It looks like you are still missing a colon. Note that the error is even pointing you to the final "f" as the point of invalid syntax. Every single line you have that begins with "with open" needs to end with a ":" for it to be valid syntax.
Reply
#6
sorry i have printed out the wrong error

Error:
File "C:\Users\Erminia\Desktop\QuizWorld\risultati\game.py", line 20 f.write(str(a1\n)) ^ SyntaxError: unexpected character after line continuation character
Reply
#7
Show your new code after you added the colons.
Recommended Tutorials:
Reply
#8
here

import sys

print("**********************")
print("*welcome to QuizWorld*")
print("**********************")
print("       v.1.0.0        ")

print("rules: Do not cheat each answer will give you 5 points, if you get more then 15 points you win if you get less you loose, after you win or lose go check out your answers and final points on lista risultati or punteggio finale")
def yes():
	a = input("do you want to play? ")
	if a == "yes":
		del a
		print("ok, so first question")
		print("whats the capital of italy?")
		a = input("answer: ")
		if a == "rome":
			print("correct")
			a1 = 5
			with open("lista risultati.txt", 'w') as f:
				f.write(str(a1\n))
		else:
			a1 = 0
			print("wrong")
			with open("lista risultati.txt", 'w') as f:
				f.write(str(a1\n))
		f.close
		print("second question")
		print("1*2*3*4*5*6*7*8*9*0*1*1*0*1")
		b = input("answer: ")
		if b == 0:
			print("correct")
			b1 = 5
			with open("lista risultati.txt", 'w') as f:
				f.write(str(b1\n)
		else:
			b1 = 0
			print("wrong")
			with open("lista risultati.txt", 'w') as f:
				f.write(str(b1\n)
		f.close
		print("third question")
		print("does it weight more 1 pound of steel or 1 pound of feathers")
		c = input("answer: ")
		if c == "they weight the same":
			print("correct")
			c1 = 5
			with open("lista risultati.txt", 'w') as f:
				f.write(str(c1\n))
		else:
			c1 = 0
			print("wrong")
			with open("lista risultati.txt", 'w') as f:
				f.write(str(c1\n)
		f.close
		print("FINAL question")
		print("is this program good?")
		d = input("answer: ")
		if d == "yes":
			print("correct")
			d1 = 5
			with open("lista risultati.txt", 'w') as f:
				f.write(str(d1))
		else:
			d1 = 0
			print("wrong")
			with open("lista risultati.txt", 'w') as f:
				f.write(str(d1))
		f.close
		
		e = a1 + b1 + c1 + d1
		
		with open("risultato finale.txt", 'w') as f1:
		f1.write(str(e)
		f1.close
		
		r = open("risultato finale.txt", "r")
		
		if r == 15:
			print("testing results...")
			print("BRAVO, you have winned the game")
		else:
			print("testing results...")
			print("you have lost the game")
		
		
			

	elif a == "no":
		sys.exit("you said to do it")
	else:
		print("!!ERROR: you need to enter yes or no. !!")
		yes()
yes()
Reply
#9
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:

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.
Reply
#10
(Oct-15-2020, 12:38 PM)GOTO10 Wrote: 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:

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.

ok, so with line 30 i need to do something like this
if b == "0"
?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,122 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  SyntaxError: invalid syntax ?? korenron 15 5,577 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 7,569 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,846 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,095 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,177 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 2,704 May-04-2021, 05:15 PM
Last Post: Yoriz
  Invalid syntax error - need help fixing calgk01 3 3,229 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Invalid syntax using conditionals if - else jperezqu 1 2,296 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  invalid syntax in line 5. Help Asadzangibaloch 2 2,352 Dec-10-2020, 04:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020