简体   繁体   中英

Python random.choices() query

import random

a_list = [1,2]
dist = [0.9,0.1]
random_number = random.choices(a_list,dist)
print(random_number)

Is it possible to get the output of this as an int? I am building a game of cricket where I need the random choice to be added the the players current score but cannot as Py cannot cannot perform additions on list and get TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' .

When called without a k argument, random.choices() will return a list containing one element. You can then withdraw that one element like you'd access any other list element.

random_number = random.choice(a_list, dist)[0]

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