# Payroll Program with Input Validation # Ch 7, Ex 1 # Written by Greg Lamb # Last modified: 05/04/2015 #Variables payRate=0 hoursWorked=0 grossPay=0 payRateGood = False numTries = 0 hoursWorkedGood = False while(payRateGood==False and numTries <3): numTries += 1 # Get hourly payrate in range $7.50-$18.25 payRate = float(input("Enter hourly pay rate: ")) # Validate payRate input if (payRate <7.5 or payRate >18.25): print("Incorrect entry - Please enter a pay rate between $7.50-$18.25") if numTries >=3: payRate = 12 else: print("The pay rate is: ${:,.2f}".format(payRate)) payRateGood = True numTries = 0 while(hoursWorkedGood==False and numTries <3): numTries += 1 # Get hours worked in range 0-40 hours hoursWorked = float(input("Enter hours worked: ")) #Validate hourWorked input if (hoursWorked <0 or hoursWorked >40): print("Incorrect entry - Enter a number of hours worked between 0 -40 hours") if numTries>=3: hoursWorked = 40 # If entry is out of range, ask user to re-enter the correct number of hours else: print("The number of hours worked is: {}".format(hoursWorked)) hoursWorkedGood = True # Caluculate gross pay def calcGrossPay(payRate, hoursWorked): return hoursWorked * payRate #Result print("Gross Pay is: ${:,.2f}".format(calcGrossPay(payRate, hoursWorked)))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question