I want to get two words but I only get one. What should I change? Here's the code:
import random
puntos = ['Norte' , 'Sur' ,'Este' , 'Oeste']
res = random.choice(puntos) #choice returns without [''], choices returns with ['']
print(res)
Ways of printing 2 values without '[' like you want
Example 1:
import random
puntos = ['Norte' , 'Sur' ,'Este , Oeste']
res1 = random.choice(puntos)
res2 = random.choice(puntos)
print(res1, res2)
Example 2:
import random
puntos = ['Norte' , 'Sur' ,'Este , Oeste']
res1, res2 = random.choices(puntos, k=2)
print(res1, res2)
Example 3:
import random
puntos = ['Norte' , 'Sur' ,'Este , Oeste']
res = random.choices(puntos, k=2)
print(res[0], res[1])
Example 4: Concatenating two words into one string
import random
puntos = ['Norte' , 'Sur' ,'Este , Oeste']
res = random.choice(puntos) + ' ' + random.choice(puntos)
print(res)
If this does not answer your question then I need a clearer question.
Always remember to check the documentation: https://docs.python.org/3/library/random.html#random.choice
random.choices()and specify the number of words to choose.res = random.choices(puntos, k=2). If you want the choices to be unique,res = random.sample(puntos, 2).k-length, so you could destructure likea, b = random.sample(puntos, 2), whereas printingresjust prints out the entire list (or what Python defaults to be the string representation of a list).