The Python Oracle

'x' is an invalid keyword argument for this function

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 Question
01:12 Accepted answer (Score 11)
01:58 Answer 2 (Score 4)
03:06 Answer 3 (Score 0)
03:24 Answer 4 (Score 0)
03:42 Thank you

--

Full question
https://stackoverflow.com/questions/1147...

Accepted answer links:
[raw_input()]: http://docs.python.org/library/functions...
[int()]: http://docs.python.org/library/functions...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python27

#avk47



ACCEPTED ANSWER

Score 11


In place of this:

int(x = raw_input('x:\n')) #max number

try this:

x = int(raw_input('x:\n')) #max number

similarly for the other input statements.

Built-in Python function raw_input() "reads a line from input, converts it to a string ". In order for you to use to use the input as an integer, you need to convert the string to int with the help of the int() function which converts "a string or number to a plain integer". From your code it looks like you had the basic idea, but your syntax was a bit tangled up.




ANSWER 2

Score 4


Unlike in C, Python assignment statements can't be used as expressions in arguments.

For example, the following simply isn't legal, and will result in a SyntaxError:

if name = name + 1:
    pass

The reason you are receiving a TypeError instead of a SyntaxError in this case is because of Python's keyword argument feature. Python allows you to pass named arguments to functions in the form: foo(arg1=0, second_argument="hello"). Thus the interpreter thinks that this is what you're trying to do.

The error message your receiving is the result of the Python interpreter thinking that you are passing a keyword argument to int(). int(), of course, does not take the keyword argument "x" (or "y" or "z" for that matter), hence the error. This is what you should do instead:

x = int(raw_input("x:\n"))
y = int(raw_input("y:\n"))
z = int(raw_input("z:\n"))



ANSWER 3

Score 0


try this

x,y,z, = int(raw_input("x:")),int(raw_input("y:")),int(raw_input("z:"))

this is the output

>>> x,y,z, = int(raw_input("x:")),int(raw_input("y:")),int(raw_input("z:"))
x:5
y:6
z:7
>>> x
5
>>> y
6
>>> z
7



ANSWER 4

Score 0


If you ever want a shorter version, try this:

x,y,z = (int(raw_input(var+':')) for var in "xyz")

(It works the same as Joran Beasley's version)