• Register
2 votes
174 views

Problem:

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

2 Answers

1 vote

Solution:

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.
Thanks.

13 9 6
94,240 points
0 votes

Solution:

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:

"SET !TIMEOUT_STEP %{USER_TIMEOUT_STEP}d" % {'USER_TIMEOUT_STEP' = 3}

Problematic syntax:

"SET !TIMEOUT_STEP {USER_TIMEOUT_STEP}".format('USER_TIMEOUT_STEP' = 3)

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

Corrected code:

"SET !TIMEOUT_STEP {USER_TIMEOUT_STEP}".format(USER_TIMEOUT_STEP = 3)

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 3 views
3 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 4 days ago psandprop 2.4k points
0 votes
1 answer 8 views
8 views
Hello house, I would like to know how to solve this Python SyntaxError: Non-ASCII character '\xe2' in file. Thanks
asked 6 days ago psandprop 2.4k points
0 votes
2 answers 502 views
502 views
Problem : Currently I am using the Python 3.3 and I have only entered these 3 below lines: import sklearn as sk import numpy as np import matplotlib.pyplot as plt But I am facing below error: “SyntaxError: multiple statements found while compiling a single statement” What is wrong with my Python code?
asked Jan 14 jwilliam 3.9k points
1 vote
1 answer 122 views
122 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 adamSw 11.3k points