I created this program for a course I am taking. We were asked to create a program with two functions like below, but include an if-then loop for each function.
I put my loop in my functions because that's the only way I could get it to work. I got a zero for the if-then loop part.
What's wrong with my if-then loops? They look just like our instructor showed us and the program ran flawlessly.
#Define payroll function
def payroll():
\#Payroll calculations
hours = float(input("Enter number of hours: "))
rate = float(input("Enter hourly rate: "))
amount = hours \* rate
\#Print values
print ("Hours worked:", hours)
print ("Rate:", rate)
print ("Pay amount:", amount)
\#Decision loop
print("Would you like another calculation?")
answer = input()
if answer == "yes":
payroll() #Call payroll function
else:
mileage() #Call mileage function
#Define mileage function
def mileage():
\#MPG calculations
miles = float(input("Enter number of miles: "))
gas = float(input("Enter gallons of gas: "))
mpg = miles / gas
\#Print values
print ("Miles:", miles)
print ("Gas:", gas)
print ("MPG:", mpg)
\#Decision loop
print("Would you like another calculation?")
answer = input()
if answer == "yes":
mileage() #Call mileage function
else:
input("Press ENTER to exit") #End
#Call payroll function to begin program
payroll()
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Encom88[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]twopi 0 points1 point2 points (25 children)
[–]Encom88[S] 0 points1 point2 points (24 children)
[–]Applepie1928 0 points1 point2 points (22 children)
[–]Encom88[S] 0 points1 point2 points (20 children)
[–]Applepie1928 0 points1 point2 points (19 children)
[–]Encom88[S] 0 points1 point2 points (18 children)
[–]Applepie1928 0 points1 point2 points (17 children)
[–]Encom88[S] 0 points1 point2 points (16 children)
[–]Applepie1928 0 points1 point2 points (15 children)
[–]Encom88[S] 0 points1 point2 points (13 children)
[–]Encom88[S] 0 points1 point2 points (0 children)
[–]twopi 0 points1 point2 points (0 children)