Wolfie
Wolfie

Reputation: 11

Getting random words from list python

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)

Upvotes: 0

Views: 1242

Answers (1)

Yellow
Yellow

Reputation: 68

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

Upvotes: 1

Related Questions