"Invalid Syntax" error despite completely valid syntax

I get an invalid syntax error when I try to subtract an input variable from another variable, despite the syntax being completely valid. I’ve tried to use different named variables. Here is my code:

init_bal = 1000.00
bal = init_bal
name = raw_input("What is the name on your account?: ")
invalid = False
transact = True
print "Hello,", name+"!"
def banking():
	global transact
	while transact == True:
		global transact
		global invalid
		global bal
		global name
		print "Your balance is $"+str(round(bal, 2))
		type = raw_input("Are you making a deposit or withdrawal?: ")
		
		if type == "deposit" or "Deposit" or "DEPOSIT" or "D" or "d":
			global bal
			deposit = float(input("How much would you like to deposit?: $"))
			bal += deposit
			print "Your new balance is $"+str(round(bal, 2))
			invalid = False
		elif type == "withdrawal" or type == "Withdrawal" or type == "WITHDRAWAL" or type == "W" or type == "w" or type == "Withdraw" or type == "withdraw":
			global bal
			#replaced bal with test to see if it was the variable name
			test = 5000
			#user inputs how much they want to withdraw
			withdrawal = float(input("How much would you like to withdraw?: $")
			#stupid sh*t that doesn't want to work
			bal -= withdrawal
			#checks if balance is negative. If so, change the text that asks you to do another transaction, and revert the change
			if bal < 0:
				bal += withdrawal
				print "You cannot withdraw more than your balance!"
				invalid = True
			else:
				print "Your new balance is $"+str(round(bal, 2))
				invalid = False
		else:
			print "Invalid transaction!"
			invalid = True
		#asks the user if they want to do another transaction
		def again():
			if invalid == True:
				again = raw_input("Would you like to try again? Enter Y or N: ")
				if again = "Y" or "y":
						transact = True
				elif again = "N" or "n":
					transact = False
					print "Thank you", name, "for putting your trust in us."
				else:
					print "Please enter Y or N!"
					again()
			else:
				again = raw_input("Would you like to make another transaction? Enter Y or N: ")
				if again = "Y" or "y":
						transact = True
				elif again = "N" or "n":
					transact = False
					print "Thank you", name, "for putting your trust in us."
				else:
					print "Please enter Y or N!"
					again()
		again()
banking()

(I have Python 2.7, so the issue could be that it’s outdated)
EDIT: Thank you everybody!

Okay, and where does it say the error is?

  File "C:\Users\7littlecj\OneDrive - Harper Creek Community Schools\Documents\Intro to Programming\Python\Python Unit 3\Exercises\L3 Banking.py", line 30
    bal -= withdrawal
      ^
SyntaxError: invalid syntax
>>>

Ah, yes. Have a look at the previous line and count the parentheses.

Incidentally, as far as the logic goes:

That means what you want it to mean;

but this does not.

(And yes, you really should upgrade.)

The problem in the uncommented previous line, which would be line 28 which is above the comment s slur.

withdrawal = float(input(“How much would you like to withdraw?: $”)
To fix this we need to add the required expected parathesis to the syntax

withdrawal = float(input(“How much would you like to withdraw?: $”))

Others have explained the actual syntax error, but I’m going to point out something that’s a bit more insidious. Yes, you absolutely SHOULD update to a newer Python, and here’s why:

name = raw_input("What is the name on your account?: ")
# ... later ...
type = raw_input("Are you making a deposit or withdrawal?: ")
# ... and later still ...
deposit = float(input("How much would you like to deposit?: $"))

This is good, good, and… nope, not good. It’s sneakily CLOSE to being correct, but actually is going to eval() whatever the user types in. If you switch to Python 3, the input() function will do exactly what you want.

My recommendation? Start adding some compatibility code to the top of your program. Add these lines, one at a time, and make sure your code works (or fix it until it does). These will ease the transition to Python 3.

from __future__ import division # make 5/2 give you 2.5 instead of 2
from __future__ import print_function # make print() into a function, with a lot more flexibility
input = raw_input # in Python 3, reverse this and say "raw_input = input" instead

Using print as a function should be pretty easy. You’ll need to add parentheses; for example print "Hello,", name+"!" could become print("Hello,", name+"!") etc; but for the most part, your code looks like it won’t have any issues. Making input and raw_input do the same thing will actually fix your code and make it safer, so that should be easy. The only one to be careful of - and it shouldn’t be a problem in this particular script - is the change to the division operator; if you expect floor division, you’ll have to adjust. But it’s worth doing. (There’s a new // floor division operator for the times when you want the Py2 / behaviour.)

The final thing worth checking is this, and it’s the same in Py2 and Py3:

if type == "deposit" or "Deposit" or "DEPOSIT" or "D" or "d":
elif type == "withdrawal" or type == "Withdrawal" or type == "WITHDRAWAL" or type == "W" or type == "w" or type == "Withdraw" or type == "withdraw":

One of these works, but takes lot of words to do it. The other doesn’t work. I suggest replacing both of them with:

if type.lower() in ("deposit", "d"):
elif type.lower() in ("withdrawal", "w", "withdraw"):

This will make it WAY easier to read, and it’ll also do what you actually expect :slight_smile:

Hope that helps!

2 Likes

Christian, and other beginners. The python parser is extensively tested. One can be something like 99.9999% sure that reported syntax errors are valid. Also, please reduce examples to the minimum needed to get the claimed misbehavior. Doing so will often reveal the answer.

Also use modern python to get better error messages. In 3.10+:

>>> withdrawal = float(input("How much would you like to withdraw?: $")
... bal -= withdrawal
  File "<stdin>", line 1
    withdrawal = float(input("How much would you like to withdraw?: $")
                       ^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
1 Like

I wish I could update, but I use school-provided software, so everything is outdated and takes months if not years for the tech people to update the software. Thanks anyways, though!

Ugh, that sucks. Maybe someone will take notice if they’re aware that Python 2.7 has not even had security upgrades since 2020?

Anyhow. You’ll definitely want to do the changes I made, which will make your code (still running under Python 2.7) more compatible with Python 3.