how to use if statement to choose from three choices

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • broception
    New Member
    • Feb 2013
    • 4

    #1

    how to use if statement to choose from three choices

    I'm trying to get the user to enter what shift they worked, if they enter first shift then the pay rate would be 12.30, if they enter second shift pay rate is 14, and so on.
    What I have so far:

    Code:
    firstShift=12.30
    secondShift=14.20
    thirdShift=15.30
    tax = .28
    otMultiplier = .5
        
    def main():    
        name = getName()
        hrsWrkd = int(input("Enter hours worked: "))
        shift = getShift()
        payRate = getPayRate(shift)
        display(name,shift)
    
    def getName():
        name=input("Enter employee name: ")
        return name
        
    def getShift():
        shift = input("Enter shift worked: ")
        return shift
    
    def getPayRate(shift):
        payRate = firstShift
        if shift==firstShift:
            return shift
        if shift==secondShift:
            return 
    
        
    def display(name,shift):
        print("The pay rate for", name)
        print("Working shift",shift,"is",payRate)
    
    main()
    The part I need help with is the getPayRate part, I'm not sure how choose from the three work shifts.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I would use a dictionary to contain the pay rates, and return the rate or None which would indicate an invalid answer.
    Code:
    dd = {'first':12.3, 'second':14.0, 'third': 15.3}
    
    def getPayRate(shift):
        return dd.get(shift.lower(), None)
    
    print getPayRate('THIRD')
    print getPayRate('xxx')

    Comment

    Working...