+ 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
4 Antworten
+ 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)
+ 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 :)
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 ?
0
oh ! I get it now. Thanks a lot Happy and everyone. :-)