0

everyone! I'm new at programming and I haven't been able to find a proper answer for this question. I'll try to explain it as best as I can:

I'd like to know if there are any ways to shorten up my code, in order to not make it so repetitive in cases like the one below.

class vehicle:
    colour = 0
    turbo = 0
    quality = 1
    type = 0

    def create_vehicle(self, pick_colour, add_turbo, define_quality, pick_type):
        self.colour = pick_colour
        self.turbo = add_turbo
        self.quality = define_quality
        self.type = pick_type
        return (self.colour, self.turbo, self.quality, self.type)

#This is the part I want to shorten up#

    def check_vehicle(self):
        if self.type == 0:
            self.type = "motorbike"
        elif self.type == 1:
            self.type = "car"
        elif self.type == 2:
            self.type = "van"
        if self.quality == 1:
            self.quality = "basic"
        elif self.quality == 2:
            self.quality = "normal"
        elif self.quality == 3:
            self.quality = "good"
        elif self.quality == 4:
            self.quality = "superior"
        if self.colour == 0:
            self.colour = "white"
        elif self.colour == 1:
            self.colour = "red"
        elif self.colour == 2:
            self.colour = "yellow"
        elif self.colour == 3:
            self.colour = "blue"
        elif self.colour == 4:
            self.colour = "green"
        elif self.colour == 5:
            self.colour = "black"
        elif self.colour == 6:
            self.colour = "orange"
        elif self.colour == 7:
            self.colour = "grey"
        if self.turbo == 0:
            self.turbo = "does not have"
        elif self.turbo == 1:
            self.turbo = "has"
        print("The %s I've created has a %s quality. It is %s and %s turbo" % (self.type, self.quality, self.colour, self.turbo))

"""
Types:
    0 = Motorbike
    1 = Car
    2 = Van
Quality:
    From 1 to 4
    Increases general stats of the vehicle (Speed, Appearance, Maneuverability)
    1 = Basic
    2 = Normal
    3 = Good
    4 = Superior
Colour:
    0 = White
    1 = Red
    2 = Yellow
    3 = Blue
    4 = Green
    5 = Black
    6 = Orange
    7 = Grey
"""

vehicle1 = vehicle()
vehicle1.create_vehicle(5, 1, 4, 0)
vehicle1.check_vehicle()

As I've checked myself, this code outputs the following:

The motorbike I've created has a superior quality. It is black and has turbo

It works! Yay! However, the problem is that I want to shorten up a part of the code, which I highlighted above. Thanks for your help

Torbald
  • 3
  • 1
  • 1
    Perhaps this is a silly question: why not skip the numbers entirely and just do `vehicle1.create_vehicle("black", "has", "superior", "motorbike")`? – Kevin Mar 01 '19 at 18:29
  • 1
    In addition to @Kevin's suggestion (which is specific to your particular implementation) it sounds like you should look into Python dictionaries. They are data structures used to map one value to another, thus allowing you to avoid many if-else situations. – HFBrowning Mar 01 '19 at 18:33
  • Usage example: `color = {0: "white", 1: "red", 2: "yellow", 3: "blue", 4: "green"}; print(color[0])` – HFBrowning Mar 01 '19 at 18:35
  • This might be more appropriate for https://codereview.stackexchange.com/ though they tend to be even more strict that [SO]. –  Mar 01 '19 at 19:02

2 Answers2

3

If you're using Python 2.7 you'll want to inherit from Object. i.e.

class vehicle:

should become

class vehicle(Object):

For info on why see Python class inherits object

Depending on your use case it might be better to define a constructor (aka init) rather than create_vehicle(..).

For more info see https://micropyramid.com/blog/understand-self-and-init-method-in-python-class/

For the check_vehicle I'd simplify by using a dictionary. e.g.

types = {
    1: "motorbike",
    2: "car",
    3:......etc
}

then in

def check_vehicle(self):
    if self.type in types:
        self.type = types[self.type]
    else:
        print "My error message"

https://www.w3schools.com/python/python_dictionaries.asp

Shane Gannon
  • 6,770
  • 7
  • 41
  • 64
0

You could use lists or dictionaries to transpose the codes into descriptions. I would suggest however that you avoid updating the members of your objects if you merely want to print a description.

def check_vehicle(self):
    types     = [ "motorbike", "car", "van" ]
    qualities = [ "", "basic", "normal", "good", "superior" ]
    colours   = [ "white", "red", "yellow", "blue", "green", "black" ]
    hasTurbo  = ["does not have","has"]
    self.type    = types[self.type]
    self.quality = qualities[self.quality]
    self.colour  = colours[self.colour]
    self.turbo   = hasTurbo[self.turbo]
    print("The %s I've created has a %s quality. It is %s and %s turbo" % (self.type, self.quality, self.colour, self.turbo))
Alain T.
  • 40,517
  • 4
  • 31
  • 51