简体   繁体   中英

“TypeError: choice() takes exactly 2 arguments (4 given)” error

I'm learning new to python and I'm making a game and I had a problem in terminal of linux like you see below:

Traceback (most recent call last):
File "oyun.py", line 30, in <module>
Pc=random.choice("tas","kagit","makas")
TypeError: choice() takes exactly 2 arguments (4 given)

Can you tell me what is the problem and what can i do?

You need to feed it a list. For example:

Pc=random.choice(["tas","kagit","makas"])

If you look at the documentation, you'll see that the method random.choice takes one argument, a seq containing the values from which to choose. So, in this case, wrap the possible choices into a list.

One more (important) thing: Even though it looks like you provided three arguments, interpreter is complaining about four (and asking for two). That is because the object upon which the method is called (in this case "random") is the implicit first argument to the method.

HTH, --ag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM