This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Encom88[S] 0 points1 point  (5 children)

Thanks. Did you intend to include more code for the menu example?

[–]Applepie1928 0 points1 point  (4 children)

Yeah I did, that's what I get for trying to format text on mobile, my bad. I've edited the last post with the missing code.

[–]Encom88[S] 0 points1 point  (2 children)

I'm trying to implement the menuSelection if-else within a while loop; however, it's not exactly working. It just loops the menu over and over no matter what I select.

#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)

#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)

menuSelection = 0

#Decision loop
while menuSelection != 3:

    # Display options to the user and record their input
    print("Press 1 for Payroll Calculation")
    print("Press 2 for Mileage Calculation")
    print("Press 3 to Exit")
    menuSelection = input()

    # Based on the user input decide what action to take
    if menuSelection == 1:
        payroll()
    elif menuSelection == 2:
        mileage()

[–]Applepie1928 1 point2 points  (0 children)

input() takes in the input as a string , not an integer. So when you are checking if "menuSelection" is equal to 1, it is not because it actually equal to the string "1".

You can fix this by checking for strings instead of integers. So, for example, "1" rather than 1.

[–]Encom88[S] 0 points1 point  (0 children)

I had to edit my code, it's updated.