Python Forum

Full Version: using class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I did my best to get my goal, I didn't get the moveUp by adding 10.

this is my code

class Rocket ():
    def __init__(self,name):
        self.name=name
        self.height=10
     
    def moveUp (self):
        self.height+=10
 
 
nameOfRockets=['Amanda', 'Atlast', 'Thores', 'Scoute', 'Domini']
fleetOfRockets=[]
for eachRocket in nameOfRockets:
    fleetOfRockets.append(Rocket(eachRocket))
for eachRocket in fleetOfRockets:
    eachRocket.moveUp()
    print('Rocket: '+ eachRocket.name + '  Height: ',eachRocket.height)
this is my goal

Rocket: Amanda Height: 10
Rocket: Atlast Height: 20
Rocket: Thores Height: 30
Rocket: Scoute Height: 40
Rocket: Domini Height: 50

what I need to do? to add height of 10 in every rocket
Post your assignment verbatim
Sorry for my english, I just need to get the goal of displaying rocket's name and their height, increasing by 10.
That is what you are doing - increasing each rocket height by 10 in a loop and displaying the result. All rockets start from 10, so the final height is 20.
That's why I ask you to post the actual assignment - what you explain in words and what you display as "expected result" does not match - e.g. the expected result suggest that first one in the list does not move up at all. It's unclear do you have to move the rocket by different distance, or e.g. simply initialize them with different height,
this is the result I got

Rocket: Amanda Height: 10
Rocket: Atlast Height: 10
Rocket: Thores Height: 10
Rocket: Scoute Height: 10
Rocket: Domini Height: 10
nope
your code will produce
Output:
Rocket: Amanda Height: 20 Rocket: Atlast Height: 20 Rocket: Thores Height: 20 Rocket: Scoute Height: 20 Rocket: Domini Height: 20
oowww my bad, that's right. What am I suppose to do to make it increase by 10 starting 10 from the first top?
Are you required to have the moveUp method? Because the way I would do it is to have two parameters for Rocket.__init__: name and height. Then before the loop where you create the rocket instances, set a variable named height to 10. Then each time through the loop you: create an instance, passing the name and the height variable as arguments, and you increase the height variable by 10.
maybe we we're not required, what was I did is just a 1st attempt. Would you share me your ideas pls?
(Dec-20-2019, 01:53 PM)Carloggy Wrote: [ -> ]Would you share me your ideas pls?

(Dec-20-2019, 01:30 PM)ichabod801 Wrote: [ -> ]the way I would do it is to have two parameters for Rocket.__init__: name and height. Then before the loop where you create the rocket instances, set a variable named height to 10. Then each time through the loop you: create an instance, passing the name and the height variable as arguments, and you increase the height variable by 10.
Pages: 1 2