using class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: using class (/thread-23295.html) Pages:
1
2
|
using class - Carloggy - Dec-20-2019 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 RE: using class - buran - Dec-20-2019 Post your assignment verbatim RE: using class - Carloggy - Dec-20-2019 Sorry for my english, I just need to get the goal of displaying rocket's name and their height, increasing by 10. RE: using class - buran - Dec-20-2019 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, RE: using class - Carloggy - Dec-20-2019 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 RE: using class - buran - Dec-20-2019 nope your code will produce
RE: using class - Carloggy - Dec-20-2019 oowww my bad, that's right. What am I suppose to do to make it increase by 10 starting 10 from the first top? RE: using class - ichabod801 - Dec-20-2019 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. RE: using class - Carloggy - Dec-20-2019 maybe we we're not required, what was I did is just a 1st attempt. Would you share me your ideas pls? RE: using class - buran - Dec-20-2019 (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. |