Home > Software engineering >  how can I use multiple else statements?
how can I use multiple else statements?

Time:07-04

I want to use a few else statements in my class method:

class Musician :
    def __initiate__(self,name, instrument, years_playing, practice_hours):
        self.name = name
        self.instrument =instrument
        self.years_playing = years_playing
        self.practice_hours = practice_hours
        # self.level = int(self.practice_hours*self.years_playing)

    def level(self):
        if self.practice_hours <= 1 and self.years_playing <=1:
            print (self.name,'is a beginner')
        else:
            self.practice_hours <=2 and self.years_playing <=2
            print (self.name,'is an intermediate')
        else:
            self.practice_hours <= 3 and self.years_playing <=3
            return (self.name, 'is an expert')  

player_1 = Musician('Don', 'guitar', 1,3)       

player_1.level()

CodePudding user response:

There are many things wrong with your code. The actual question you have seems to be about using elif where you tried to use else; the general structure is

if condition:
    something should happen
elif other_codition:
    things which should be done in this case
elif third_condition:
    other things
else:
    unconditionally do this if none of the conditions above were true

where all the branches after the first are optional.

You are mixing print with return, and I'm guessing you meant __init__. Here's an attempt to fix the code.

class Musician:
    def __init__(self, name, instrument, years_playing, practice_hours):
        self.name = name
        self.instrument =instrument
        self.years_playing = years_playing
        self.practice_hours = practice_hours
        # self.level = int(self.practice_hours*self.years_playing)

    def level(self):
        if self.practice_hours <= 1 and self.years_playing <= 1:
            return "beginner"
        elif self.practice_hours <= 2 and self.years_playing <= 2:
            return "intermediate"
        elif self.practice_hours <= 3 and self.years_playing <= 3:
            return "expert"

player_1 = Musician('Don', 'guitar', 1, 3)       

lvl = player_1.level()
print(f"the level of {player_1.name} is {lvl}")

__init__ is a reserved name for the method which gets invoked when you create a new instance; you can't use a different name and hope for it to be invoked under the shme circumstances.

The level function will still return None if none of its conditions are true, but how to fix that depends on criteria I can't know. Perhaps a better design would be to check for "expert" or "intermediate" and then otherwise always return "beginner" if neither of those are true.

    def level(self):
        if self.practice_hours > 2 and self.years_playing > 2:
            return "expert"
        elif self.practice_hours > 1 and self.years_playing > 1:
            return "intermediate"
        # else
        return "beginner"

CodePudding user response:

You can use bisect to achieve the same, without using if

import bisect

def level(self):

    level_dict = {0 : 'is a beginner', 1 : 'is an intermediate', 2 : 'is an expert'}
    grade_ranges = [(0, 1), (1, 2), (2, 3)]
    points, grade = zip(*grade_ranges)
    return self.name   level_dict[bisect.bisect(points, self.practice_hours)-1]
  • Related