I am trying to write a text adventure game and I need some help. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I am trying to write a text adventure game and I need some help.

I have created some objects (heroes) and I want them to be chosen randomly. But I don't know how to do that. Can someone help me please ? https://code.sololearn.com/cP1YFii5J5U4/?ref=app

19th May 2018, 4:45 PM
Masquerade
Masquerade - avatar
4 Answers
+ 3
You could put your heroes in a list and use random choice. Like this: import random h_choice = [Hero_1, Hero_2, Hero_3] x = random.choice(h_choice ) print(x)
19th May 2018, 5:00 PM
Paul
Paul - avatar
+ 2
First off, great start! Selecting a random hero should be relatively easy. One way to do it would be: import random heroes = [Hero_1, Hero_2, Hero_3] player = random.choice(heroes) Hope this helps :)
19th May 2018, 5:01 PM
Just A Rather Ridiculously Long Username
0
Aha ! thanks a lot guys. it's working. But there's something interesting happens when I put h_choice = ['Hero_1', 'Hero_2', 'Hero_3'] x = random.choice(h_choice ) print(x) inside the init function. The x gets printed thrice. Can someone explain me how and why ?
19th May 2018, 5:48 PM
Masquerade
Masquerade - avatar
0
oh ! I get it now. Thanks a lot Happy and everyone. :-)
19th May 2018, 5:59 PM
Masquerade
Masquerade - avatar