Good morning,
I am wondering if someone can point me in the right direction.
I am trying to have a loop go through a list and add them to a printable list.
let's say the user inputs the number two: Then it should print go pull out two random class-names of the list. if the user inputs 4 it should pull out 4 random class-names from the list.
this is so i can print out attributes from the classes afterwards depending on the class-names above. with utskrift1=(vars(plane))
i have tried normal loops but it seem to print out the list in it's entirety, and if i ie go print out x=2 then it prints the entire list two times.
#The classes:
import random
class plane:
def __init__(self, dyr, dyrefamilie, antallbein):
self.dyr = 'Hund'
self.dyrefamilie = 'Hundefamilien'
self.antallbein = '4'
def __str__(self):
return f'undulat(={self.dyr},{self.dyrefamilie},{self.antallbein})'
plane2 = plane(dyr='something', dyrefamilie="something", antallbein='4')
class rocket:
def __init__(self, dyr, dyrefamilie, antallbein):
self.dyr = 'something'
self.dyrefamilie = 'something2'
self.antallbein = '4'
def __str__(self):
return f'katt(={self.dyr},{self.dyrefamilie},{self.antallbein})'
rocket2 = rocket(dyr='something', dyrefamilie="something", antallbein='4')
class boat:
def __init__(self, dyr, dyrefamilie, antallbein):
self.dyr = 'something'
self.dyrefamilie = 'something2'
self.antallbein = '5'
def __str__(self):
return f'undulat(={self.dyr},{self.dyrefamilie},{self.antallbein})'
boat2 = boat(dyr='something', dyrefamilie="something", antallbein='2')
Is it possible to randomise a selectetion and have the list.append(selected random name) instead of preselecting it like i have done below?
x2=list = []
x1=list = []
# appending instances to list
list.append(plane(1,2,3))
list.append(rocket(1,2,3))
list.append(boat(1,2,3))
random.shuffle(x1) #roterer litt rundt på listen
for i, x1 in enumerate(x1): #kode fra canvas
print('Dyret er en', x1.dyr,'med',x1.antallbein+'-bein'+'.','denne er er en del av', x1.dyrefamilie)