By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,822 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,822 IT Pros & Developers. It's quick & easy.

how to use if statement to choose from three choices

P: 4
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:

Expand|Select|Wrap|Line Numbers
  1. firstShift=12.30
  2. secondShift=14.20
  3. thirdShift=15.30
  4. tax = .28
  5. otMultiplier = .5
  6.  
  7. def main():    
  8.     name = getName()
  9.     hrsWrkd = int(input("Enter hours worked: "))
  10.     shift = getShift()
  11.     payRate = getPayRate(shift)
  12.     display(name,shift)
  13.  
  14. def getName():
  15.     name=input("Enter employee name: ")
  16.     return name
  17.  
  18. def getShift():
  19.     shift = input("Enter shift worked: ")
  20.     return shift
  21.  
  22. def getPayRate(shift):
  23.     payRate = firstShift
  24.     if shift==firstShift:
  25.         return shift
  26.     if shift==secondShift:
  27.         return 
  28.  
  29.  
  30. def display(name,shift):
  31.     print("The pay rate for", name)
  32.     print("Working shift",shift,"is",payRate)
  33.  
  34. main()
  35.  
The part I need help with is the getPayRate part, I'm not sure how choose from the three work shifts.
Mar 12 '13 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
I would use a dictionary to contain the pay rates, and return the rate or None which would indicate an invalid answer.
Expand|Select|Wrap|Line Numbers
  1. dd = {'first':12.3, 'second':14.0, 'third': 15.3}
  2.  
  3. def getPayRate(shift):
  4.     return dd.get(shift.lower(), None)
  5.  
  6. print getPayRate('THIRD')
  7. print getPayRate('xxx')
Mar 12 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.