0
Random Welcome Messages (python)
Let's say there are different variables such as x,y,z x = "bla" y = "blabla" z = "blablabla" I want to see just one of them randomly when I run the code, like welcome messages in Minecraft How can I do it? Thanks in advance.
9 Respostas
+ 5
create a list
r=[x,y,z]
import random
print(random.choice(r))
but it's easy to find with a Google recherche...
+ 3
instead of x="" y="" z="" ... just make a list of the possible welcome messages.. These top 3 lines will do what you want.
#Import random module
import random
#Make a list of the messages
x=["Welcome","Enter if you dare","Go away"]
#use random.choice() to print one
print(random.choice(x))
#Just for fun
#10 random picks from the list
for i in range(10):
print(random.choice(x))
+ 2
Yeah, except defining x,y,z just writing directly is more practical
+ 1
thanks dude, you are right I shouldn't be that lazy to look up. I'll terminate this post later
+ 1
no need to terminate it. It was the type of questions this forum is here for.
+ 1
By the way, I defined x,y,z's like x = "Welcome back" and wrote the rest of the code as hbeo indicated. It printed out 'no output' once. Why might that have happened ?
+ 1
I've modified the code...
0
I'd like to ask other questions related to greeting messages but I haven't finished my course yet, thanks for the comments!
- 1
the code without problems
https://code.sololearn.com/c8gq4d1C2SlE/?ref=app