Home > Mobile >  Adding iterations to a list depending on input
Adding iterations to a list depending on input

Time:12-01

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)

CodePudding user response:

Use the random std package, just put your instantiated objects in a list and you can choose a random one like such:

from random
values = [1,2,3,4,5,6]
rand_val = random.choice(values)

CodePudding user response:

You can do a ranged for, which means that the for will execute x times (range(x)). By this way now you have a for executing X times, and you can index in the list, for example, the current iteration 'i' and print its object properties.

You have to take care about the range(x) and the range of the list/array you are indexing, if the range(x) > range(list) then you will have out of bound errors.

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 in range(2):
    print('Dyret er en', x1[i].dyr,'med',x1[i].antallbein '-bein' '.','denne er er en del av',   x1[i].dyrefamilie)
  • Related