python - choosing a random number from a tuple + some numbers -
i have tuple of numbers, , want choose random number tuple in addition number. example:
my_tuple = (1,2,3)
and have number 4. want choose random number numbers 1,2,3,4 (without changing tuple of course).
i tried:
my_tp = (1, 2, 3) = random.choice(list(my_tp).append(4)) print(a)
i'm new python. tried converting tuple list, , performing random function. code above didn't work. got error
object of type 'nonetype' has no len()
would love help.
list.append
returns none
once converting list
have done, appending modify list
return none
source of error
.
to round can either convert tuple
list
append 4
it, use random.choice
, or in 1 step, can concatenate list of [4]
+
operand.
this approach simpler:
import random my_tuple = (1,2,3) random.choice(list(my_tuple) + [4])
hope helps , clears things up! :)
update:
if want randomly select tuple without last item, slice
list normal syntax:
random.choice(list(my_tuple)[:-1])
Comments
Post a Comment