• Register
2 votes


I have a simple python program I wrote recently. But unfortunately, I can’t compile and run my program. It shows me an error “syntaxerror: keyword can't be an expression
Where I am being wrong in my program? Any idea?

The snippet is attached below:

name = input("Please put your name here ")

if name == "Gavin": print(name + " is a Gentleman!")

else: print(name + " I don't know him and " + name = " Who is he? ") 

Thanks, guys.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote


You’ve made the mistake in your else part. When you wrote:

else: print(name + " I don't know him and " + name = " Who is he? ") 

the program is trying to assign a new string under a variable in a print line. That’s not possible man. You need to use the name as a variable and put the = as part of the String.

else: print(name + " I don't know him and " + name + = " Who is he? ") 

Now, this looks good and should run fine.

13 9 6
94,240 points
0 votes


sum.up is not a valid keyword argument name. Keyword arguments must be valid identifiers. You must look in the documentation of the library you are using how this argument really is called – maybe sum_up

I guess many of us who came to this page have a problem with Scikit Learn, one way to solve it is to create a dictionary with parameters and pass it to the model:

params = {'C': 1e9, 'gamma': 1e-07}
cls = SVC(**params)    

It's python source parser failure on sum.up=False named argument as sum.up is not valid argument name (you can't use dots -- only alphanumerics and underscores in argument names).

At the time you are using the Elastic search DSL API, you may hit the same error with

s = Search(using=client, index="my-index") \
    .query("match", category.keyword="Musician")

You can solve it by doing:

s = Search(using=client, index="my-index") \
    .query({"match": {"category.keyword":"Musician/Band"}})

I just got that problem when converting from % formatting to .format().

Previous code:


Problematic syntax:


The problem is that format is a function that needs parameters. They cannot be strings. 

Corrected code:


The line below is wrong. You are using "=" sign as if you were assigning name to a new string within the print function. Doesn't work that way.

else: print(name + " sleeps all night and " + name = " works all day!")

Try it like this.

You need to use name as a variable and put the = as part of the String.

else: print(name + " sleeps all night and " + name + "= works all day!")

This worked for me in the interpreter

>>> name = input("What's your name? ")
What's your name? Ryan
>>> if name == "Kenneth": print(name + " is a lumberjack and he's OK!")
... else: print(name + " sleeps all night and " + name + "= works all day!")
Ryan sleeps all night and Ryan= works all day!

You have to use == which is the equality operator, not a single = which is the assignment operator (for example a = 1 assigns the value 1 to the variable a). In addition, you can also shorten your expression by having the comparison as the first argument and dropping the Value field.

test_reclass = Float(Con("cpraster3.tif" == 1, 0.27, Con("cpraster3.tif" == 2, 1.00, Con("cpraster3.tif == 3, 2.9, Con("cpraster3.tif" == 4, etc...


10 6 4
31,120 points

Related questions

0 votes
1 answer 20 views
Problem: when I run code I get the following error: File "3.py", line 39 s1=f(x,ti,k=k1,w=w1,h[1]) SyntaxError: non-keyword arg after keyword a I partially understand what it means but im not really sure how to fix it, any idea?I am still new to python, but I assume it is something to do with using the h[] array within the for loop?
asked Feb 16 Mashhoodch 13k points
0 votes
1 answer 57 views
Problem: I wrote the following code which extracts the info. of a file and orders it alphabetically based on its second column objects: import csv import operator import sys def re_sort(in_file='books.csv', out_file='books_sort.csv'): data = csv.reader(open('books. ... function. Do you guys see reasons why this may be happening. The following if a structured version of the contents in the file
asked Feb 21 Mashhoodch 13k points
0 votes
1 answer 65 views
Problem: In a python program I am making, I want it to only take integers, and if it gets a string say "There has been an error in the system." instead of murmering sensless information the user will not understand
asked Jan 11 Mashhoodch 13k points
0 votes
1 answer 26 views
Task: Read an integer N . For all non-negative integers i<N , print i2. if __name__ == '__main__': n = int(input()) if (n<=0) and (n>=20): print("Exit") else: for(i=0,i<=n,i++): print(i*i) I am getting below error File "solution.py", line 6 for(i=0,i<=n,i++): ^ SyntaxError: invalid syntax Exit Status 255
asked Oct 25, 2020 psandprop 2.4k points
1 vote
1 answer 2 views
Problem: Someone asked me about this but I have no any clue. Ref keyword must be used while using out parameter mechanism.
asked Mar 24 PkGuy 13.1k points
0 votes
1 answer 10 views
Problem: a constant can be defined using using the final keyword. compiler showing this error. Any help will be appreciated. Thanks
asked Feb 25 jyoti goyal 2.1k points
0 votes
1 answer 1 view
1 view
Problem: Normally, I use this in constructors only. I understand that it is used to identify the parameter variable (by using this.something), if it have a same name with a global variable. However, I don't know that what the real meaning of this is in Java and what will happen if I use this without dot (.).
asked 4 days ago Sana8989 8.9k points
1 vote
1 answer 259 views
Problem: Hello programmers, I hope you all are doing good in this hard time of Covid-19. I am under lockdown inside my house for more than two months. I was being mad. I decided to learn something new in these days. Therefore, I come to an end with the decision of ... mine that don&rsquo;t work var = 123 123 = roy print(var) print(123) Please, review the codes and help this beginner to walk ahead.
asked May 31, 2020 adamSw 11.3k points
0 votes
1 answer 17 views
Problem: How to solve SyntaxError on autogenerated manage.py?
asked Feb 13 Shovo210 14k points
0 votes
1 answer 80 views
Hello house, I would like to know how to solve this Python SyntaxError: Non-ASCII character '\xe2' in file. Thanks
asked Oct 23, 2020 psandprop 2.4k points