Skip to content
Sign upLog in
← Back to Community

How do I print a random string from a list that I made?This is in python, btw

Profile icon
[deleted]

Hi everyone, just started learning python, and I don't really know what to say here, except I need help with using random.choice[] without pink and red screaming at me from the console or whatever!! Haha

Answered by RYANTADIPARTHI [earned 5 cycles]
View Answer
Comments
hotnewtop
Profile icon
RYANTADIPARTHI

Solution

you have to use the random module.

import random lst = ["something", "more something", "even more something"] print(random.choice(lst))

Like that.
That should work

Profile icon
[deleted]

ok, lemme try.thx

Profile icon
Coder100

You have to use ( to call a function. [ is used for indexing parts of an array, and random.choice is obviously not an array, it's a function!

import random german = 'deutsch' english = 'english' a = '1' b = '2' # some list print(random.choice([german, english, a, b])) # how I think I need to do it

Also don't forget to import the random module, it's not there by default.

Profile icon
SameeraMurthy

Solution

In main.py Try doing:

import random german = 'deutsch' english = 'english' a = '1' b = '2' print(random.choice([german, english, a, b]))

You needed to import the random module first.
Plus, random.choice() is a function.
Hope this helps

Profile icon
Nithish1

@SameeraMurthy

is it possible to convert
print(random.choice([german, english, a, b]))
into a variable

Profile icon
Nithish1
  • store it as a variable