Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using class
#1
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
Reply
#2
Post your assignment verbatim
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Sorry for my english, I just need to get the goal of displaying rocket's name and their height, increasing by 10.
Reply
#4
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,
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
oowww my bad, that's right. What am I suppose to do to make it increase by 10 starting 10 from the first top?
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
maybe we we're not required, what was I did is just a 1st attempt. Would you share me your ideas pls?
Reply
#10
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020