#OOP help

49 messages · Page 1 of 1 (latest)

shadow kayak
#

Hey everyone, I needed some slight help with this the first two images are examples/general ideas of what my professor wants. I have trouble with OOP sometimes. Right now I think I have some attributes set up and a class. I really want to make a train with attributes and to make inputs like in the examples. Im just a tad bit confused and I just need some slight guidance. - Thanks

weak cosmos
#

Looks like you're on the right track

#

(pun not intended)

#

Think about what actions your train can take (i.e. the methods of the train)

#

and try implement those

shadow kayak
#

Wdym?

#

How do I make the methods actually work?

pastel smelt
#

It's simple really

shadow kayak
#

I also want to be able to add an input that lets you add the conductor name, train model, passenger count, speed and color

shadow kayak
pastel smelt
shadow kayak
#

I know but I can't call them when I try

pastel smelt
#

You will need to create an object

shadow kayak
#

Example?

pastel smelt
#

Something like

class Train:
    def __init__(self, name, destination, num_cars, speed):
        self.name = name
        self.destination = destination
        self.num_cars = num_cars
        self.speed = speed
        
    def accelerate(self, amount):
        self.speed += amount
        
    def decelerate(self, amount):
        self.speed -= amount
        
    def display_info(self):
        print("Train Name:", self.name)
        print("Destination:", self.destination)
        print("Number of Cars:", self.num_cars)
        print("Speed:", self.speed)
my_train = Train("Express", "Chicago", 10, 50)
my_train.accelerate(10)
my_train.display_info()
shadow kayak
#

Ohhhh okay I get you

#

Ill give it a try later

shadow kayak
#

Everything is looking fine but the info isn't lining up too good

weak cosmos
shadow kayak
#

Ohhh as a f string type deal

shadow kayak
#

How do I get the said info to lineup?

shadow kayak
#

Good news is that I got it all to line up pretty well

#

Im just wondering how I can start the accelerating and decelerating part of it

shadow kayak
#

Im thinking I would maybe need to make a main?

#

Then call the methods in that main?

weak cosmos
#

You could do it that way

shadow kayak
#

Kinda minor update

#

I got a hypothetical where it goes to the main place

#

Also a if derailment and stop with a break

vale scaffoldBOT
#

@shadow kayak

Wackazoid Modok Uploaded Some Code
Uploaded these files to a Gist
shadow kayak
#

You guys can mess with it and see what I can add to make it look and work kinda better

#

If you want

shadow kayak
#

I want it to look a bit better ig

weak cosmos
#

That depends on you and how you want to print the stuff out

shadow kayak
#

Its hard to explain my idea

vale scaffoldBOT
#

@shadow kayak

Wackazoid Modok Uploaded Some Code
Uploaded these files to a Gist
shadow kayak
#

You look at it and see what I can change

shadow kayak
#

I guess im looking for critique functionally as well @weak cosmos

shadow kayak
#

@weak cosmos well?

weak cosmos
#

It's not too bad

#

A little bit badly formatted but functionally it should still run correctly

#

Just make sure it satisfies your requirements

shadow kayak
#

How should I format it better?

shadow kayak
#

I do ask though @weak cosmos

#

If I wanted to format it to use user_inputs for the attributes, how would I do that?

weak cosmos
#

I referred to formatting as in following PEP8