dimanche 7 octobre 2018

Using a random instance of a class in a text

I am new to python and want to write a simple text adventure game. The player enters a tavern and interacts with the guests. The game takes place in a fantasy setting, where there are multiple races. I want to randomly generate each guest and then interact with them in the tavern. Here is my simplified code:

import random
class guest:
    def __init__(self,race,name,fav_food):
        self.race = race
        self.name = name
        self.fav_food = fav_food

guest1 = guest('human','Tom','chicken')

print('The first guest you meet is a '+guest1.race+ ' named '+guest1.name+ '. He really likes '+guest.fav_food+ '.')

So far so good. But here i get stuck: I want the set of data for guest1 to be randomly selected from other guests that i create beforehand.

guest1 = guest('human','Tom','chicken')
guest1 = guest('dwarf','Bjorn','potatoes')
guest1 = guest('orc','Orok','pork')
guest1 = guest('elf',,'Eli','Salad')

How do i do that? Sure, i could name them guest2,guest3 etc., but then it wouldn´t be random anymore.

When i run the code, i want to randomly encounter Tom,Bjorn,Orok or Eli

I would really appreciate any help on this matter. Sorry for my bad english :)




Aucun commentaire:

Enregistrer un commentaire