+ 3

Hellow every one! I would like if someone will teach me how to pick a random string/integer from a list, in python3

how to use the random method for pick a random str/int from list in python3?

1st Feb 2018, 8:34 PM
GoldenCode
7 odpowiedzi
+ 8
#Toni's code doing random length from the list. #If you want to get random value from the list, you need to use choice. from random import choice nums = [1,2,3,4,5] rand = choice(nums) print(rand) # Or without variable print(choice(nums))
1st Feb 2018, 9:11 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 4
you right, thank you
1st Feb 2018, 9:12 PM
GoldenCode
+ 4
You need only change content from list to that you wrote with "g","h" and what more you want.
1st Feb 2018, 10:01 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 3
from random import randint list = [1, 2 3, 4, 5]; result = list[randint(0, len(list) - 1)]
1st Feb 2018, 8:52 PM
Toni Isotalo
Toni Isotalo - avatar
+ 3
thank you very much
1st Feb 2018, 9:07 PM
GoldenCode
+ 3
can you create a code for example? make a list with string like "h","g" and then make python choose one of them thank you!
1st Feb 2018, 9:40 PM
GoldenCode
+ 3
just like: 'from random import choise list=["g","h"] print (choise(list))?
1st Feb 2018, 10:04 PM
GoldenCode