Invalid syntax (x= is invalid in function) - python

I'm having trouble with my function/class in Python 3.3.1
This is the code:
def snell(ang1=None, ang2=None, v1=None, v2=None, n1=None, n2=None):
try:
if ang1==None and n1==None:
ang1=math.degrees(math.asin((math.sin(ang2)*v1)/v2)
n1=(n2*v1)/v2
print("ang1 is equal to:", ang1, sep='\n')
print("n1 is equal to:", n1, sep='\n')
Sorry that the code is separated weirdly, I am very new to this. In any case, the syntax error I'm getting only says "invalid syntax" and highlights the variable n1 in the line n1=(n2*v1)/v2. I am wondering what the problem could be, as I see nothing wrong with that snippit of the code. This is part of a larger section of code, but the rest contains a multitude of elif's in the same format of this if statement. If any portion of this request is atrociously written please tell me how to improve it...much appreciated.

You're missing a closing ) on this line :
ang1=math.degrees(math.asin((math.sin(ang2)*v1)/v2))

Related

File "<string>" SyntaxError: invalid syntax [duplicate]

This question already has answers here:
Why do I get the syntax error "SyntaxError: invalid syntax" in a line with perfectly valid syntax?
(4 answers)
Closed 4 months ago.
I've recently been trying to code a program which plays domino with two players and with input, but apparently my syntax is flawed at the line where I define a function. I think it might be because of my near-zero experience with coding πŸ˜…, but after I've searched on the internet, I haven't found any solution.
Here's the code :
def comptage():
occurences_of_numbers=[]
for i in range(7):
a=0
for k in range(7):
if i in list(hand[k]):
a=a+1
occurences_of_numbers.append(a)
return(occurences_of_numbers)
File "<string>", line 36
def comptage():
^
SyntaxError: invalid syntax
This function counts the numbers of occurrences of the numbers (0,1,2,3,4,5,6) in the tuples representing the dominos in the list named hand. I then use the function for other functions.
available_draws=14
opponent_hand=7
while comptage()[list(board[0])[0]]==0 and comptage()[list(board[len(board)-1])[1]]==0:
inpt_list=list(map(int,input(' Quel domino piochΓ© ?')))
M.append((inpt_list[0],inpt_list[1]))
available_draws=available_draws-1
#board is a tuple list representing the board
def riposte():
probability=[]
for i in range(7):
b=0
for k in range(len(board)):
if i in list(board[k]):
b=b+1
probability.append((7-comptage()[i]-b)/(28-len(hand)-len(board))*(opponent_hand/(opponent_hand+available_draws)))
return(probability)
This error popped up after I changed something in the riposte() function (I added the b characteristic). But it shows the mistake for the comptage() function. I don't know why it showed that.
I've tried changing the code a bit, rewriting it, and using other variants, but the error message stands still.
The rest of the code is : (some unfinished stuff)
zero=[]
one=[]
two=[]
three=[]
four=[]
five=[]
six=[]
all_the_dominos=[zero,one,two,three,four,five,six]
for i in range(7):
for k in range(7):
all_the_dominos[i].append((i,k))
hand=[]
for i in range(1,8):
input_list=list(map(int,input(' Domino '+str(i)+' ?')))
hand.append((input_list[0],input_list[1]))
print(hand)
for i in range(7):
for k in range(7):
if hand[i] in all_the_dominos[k]:
del all_the_dominos[k][all_the_dominos[k].index(hand[i])]
elif tuple(reversed(list(hand[i]))) in all_the_dominos[k]:
del all_the_dominos[k][all_the_dominos[k].index(tuple(reversed(list(hand[i]))))
board=[(6,1),(1,2),(2,6),(6,6)]
#example of a board
available_draws=14
opponent_hand=7
while input(' Pioche adverse ?')=='oui':
opponent_hand=opponent_hand+1
available_draws=available_draws-1
The only thing that comes after what is problematic is the last bit.
Help would be greatly appreciated. 😁
!! UPDATE !!
I've trisd another version of the compatge function, but it still shows the same error :
def comptage():
F=[0,0,0,0,0,0,0]
for i in range(7):
for k in range(len(M)):
if i in list(M[k]):
F[i]=F[i]+1
return(F)
But something's strange : i can't even define a variable !
a=0
File "<string>", line 34
a=0
^
SyntaxError: invalid syntax
I definitely think there is a problem with my phone because i've downloaded several other coding programs and always got the same error.
It's strange because it was working fine and all of a sudden, it doesn't allow me to do anything.
Well maybe there's just a problem with the code πŸ˜‚ but i really don't think so.
It looks like you have 1 more closing parenthesis ()) than opening parenthesis (() on this line: P.append((7-comptage()[i]-b)/(28-len(M)-len(J))*(aM/(aM+p)))
Actually, I've found the problem : there was just a missing bracket πŸ˜•. I'm new to Python so I didn't pay enough attention even though I thought I did.

Python 3.5 print syntax error in Spyder - works in console?

I'm trying to use print with .format() to create a table, but I'm getting a syntax error on:
print("|{:<40}|{:<40}|{:<40}|{:<40}|".format(*tableheads))
with tableheads being a predefined list of 4 arguments. The error I'm getting when I try to run is:
print("|{:<40}|{:<40}|{:<40}|{:<40}|".format(*tableheads))
^
SyntaxError: invalid syntax
For a while I thought I'd done something wrong, but I've looked through the documentation, and even tried it in the console where it worked perfectly. For some reason though, I'm getting a syntax error when I try to include it as part of a function.
I'm not sure what to do, and I feel like I must be missing something, but what?
Thanks in advance.

Python 3.3: separation argument (sep) giving an error

I am very new to programming and I'm starting out with Python. I tried to look up my question here but didn't really find anything.
I'm trying to work a very simple print command but I'm getting an error for some reason that I don't understand.
last = 'smith'
middle = 'paul'
first = 'john'
print(first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
According to the answer in the book, this should be right, but every time I try to run it, I get an error with the 'sep':
print(first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
^
SyntaxError: invalid syntax
Can someone tell me what I'm doing wrong. for what it's worth I'm using PyScripter.
[EDIT]
Thanks for that. I found out that I'm using Python 2.7.3 instead of 3.3. So I looked up the manual to see how the separator works. It seems to me that the only difference is with the square bracket. The manual describes the print function as :
print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout])
So I changed my print command and added the square bracket:
print ([first.capitalize(),middle.capitalize(),last.capitalize()] [, sep='\t'])
but unfortunately this doesn't work either as I get an error that highlights the square brackets around sep='\t'. Even when I take the brackets out, the error doesn't go away.
I'm not sure what I'm doing wrong, it seems like it should be very simple.
You aren't actually using Python 3, you just think you are. Try:
import sys
print(sys.version)
and see what comes out. The Python 2 print ... statement (not print(...) function in Python 3) interprets this as
print (first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
which is trying to print a tuple with a keyword argument, thus the syntax error on sep

while loop in python gives syntax error

I am very new to python, this is my first program that I am trying.
This function reads the password from the standard input.
def getPassword() :
passwordArray =[]
while 1:
char = sys.stdin.read(1)
if char == '\\n':
break
passwordArray.append(char)
return passwordArray
print (username)
print (URL)
getting this error:
Problem invoking WLST - Traceback (innermost last):
(no code object) at line 0
File "/scratch/aime/work/stmp/wlstCommand.py", line 10
while 1:
^
SyntaxError: invalid syntax
Your indentation is not correct. Your while should be indented the same as the line above it.
Python uses indentation to "separate" stuff and the thing with that is you need to have the same kind of indentation across the file. Having a fixed kind of indentation in the code you write is good practice. You might want to consider a tab or four spaces(The later being the suggestion in the PEP8 style guide)
Python is sensitive and depended on indentation. If it complains "invalid Format", that is better than "invalid syntax".

A program running my python module always returns a syntax error on the first line after a while loop

I'm a beginner at python with experience in Java having to write a python module for a Virtual robot challenge for my high schools MESA(A technology based competition) club. I have been trying to solve this weird problem for days and I only have 6 hours left to fix all the bugs in my code! The "invalid syntax" always occurs on the first line after a while loop here is the relevant information:
Keep in mind that values have been properly added to the lists
Relevant code:
interestlengthl=list()
interestlengthr=list()
interestpoint=list()
def do_examine(robot):
examinecount=0;
while (examinecount<(max(interestpoint)) <-the while loop
i=2+2 <-a innocent line used as an example, this returned an invalid syntax
maxpoint=max(interestpoint)
tomove=(currentposition-(max[interestpoint]-interestpoint(examinecount)))
robot.step_forward(tomove)
leftscan=robot.sense_steps(robot.SENSOR_LEFT)
rightscan=robot.sense_steps(robot.SENSOR_RIGHT)
if (rightscan==interestlengthr(examinecount):
robot.turn_right()
do_rowscan(robot)
if (leftscan==interestlengthl(examinecount):
robot.turn_left()
do_rowscan(robot)
examinecount+=1
robot.turn_right(2)
currentposition=robot.sense_steps(robot.SENSOR_FORWARD)
robot.turn_right(2)
Relevant error:
File "L:\controllers\controller_zigzag.py", line 35
i=2+2
^
SyntaxError: invalid syntax
While loops should have a colon on them, such as with:
while examinecount < max (interestpoint):
just like your if statements further down. And, as an aside, it's not C - you don't need parentheses around the entire conditional.
The syntax error is that you must place a colon after the loop, AND you have unbalanced brackets: while (examinecount<(max(interestpoint)) -> while (examinecount<(max(interestpoint))):

Categories