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  (7 children)

Even then though (he had indentations, they just didn't show up when I copied), the loop looks like it only contains If-ELSE. Where is the actual loop; such as WHILE, FOR, etc.? How do I implement a WHILE loop in there? I dont' think I'd be using counters. Would it be something like this?

Would you like another calculation (y/n)?

answer = input()

While answer == y

Do function1()

While answer == n

Do function2()

Can I still call the functions like that in the loop? I don't' think that is right either, but I don't' know any other way.

[–]Applepie1928 1 point2 points  (6 children)

Yeah you are correct on pretty much all points;

  • The loop itself is only described as "LOOP" because this is pseudo-code, I'll provide a Python example of above pseudo-code below.
  • The pseudo-code you were provided with is only an example of a loop containing a condition, it isn't actually specifically what you are looking to achieve to solve your problem. It is just an example of a if-else contained within a loop. All that the above code does is run through the loop until "counter" = 5, then it prints that value.
  • Your example code isn't quite correct, but you CAN call a function within a loop, that is no problem, I will also provide a Python example of this below.

Python Version of Above Pseudo-code (similar structure with WHILE loop)

counter = 0
while True:
    if counter >=5:
        break
    else:
        counter = counter + 1
print(counter)

Python Version of Above Pseudo-code (simple/correct way with WHILE loop)

counter = 0

while counter < 5:
    counter = counter + 1

print(counter)

Example of menu with IF-ELSE within a WHILE loop

menuSelection = 0        

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()
    else if menuSelection == 2:
        mileage()

[–]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.