0

Until this semester I didn't even know a while True was a thing. I have to write a while True loop to loop until the user enters 'n' to break. My problem is restarting the loop if the user enters anything other than 'y' or 'n'. Currently, I can loop with any character besides 'n'. I need a way to catch the if say 'q' was entered, "please enter 'y' or 'n'" and prompt the user again. I have considered doing another while loop within the loop but I feel like there is a more optimal way to achieve this.

def main():
    userinput = "y"
    display_title()
    while True:
        userinput.lower() == "y"
        choice = ""
        display_menu()
        choice = str(input("Select a conversion (a/b): "))
        while choice == "a" or "b":
            if choice == "a":
                print()
                feet = int(input("Enter feet: "))
                meters = conversions.to_meters(feet)
                print(str(round(meters, 2)) + " meters")
                print()
                break
            elif choice == "b":
                print()
                meters = int(input("Enter Meters: "))
                feet = conversions.to_feet(meters)
                print(str(round(feet, 2)) + " feet")
                print()
                break
            elif choice != "a" or "b":
                print("Please enter a valid option a or b")
                choice = str(input("Select a conversion (a/b): "))
        userinput = input("Would you like to perform another conversion? (y/n): ")
        if userinput == "n":
            print()
            print("Thanks, Bye!")
            break
Mwheeler91
  • 215
  • 2
  • 8
  • One way of saying it's true is 'not False'. In your case, False would be 'n'. – hrokr Sep 01 '20 at 19:29
  • 1
    One issue I see in this code, see [How to test multiple variables against a value](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – G. Anderson Sep 01 '20 at 19:30
  • `while choice == "a" or "b":` does not do what you think it does (it's always `True`) – Cyril Jouve Sep 01 '20 at 19:37

1 Answers1

0

You don't need another while loop. You could just need to put the input check to the beginning of the loop and add a check for any other character than 'y' and 'n', e.g.:

def main():
userinput = "y"
display_title()
while True:
    if userinput == "n":
        print()
        print("Thanks, Bye!")
        break
    elif userinput != "y":
        userinput = input("Please select yes (y) or no (n)").lower()
        continue
    ### If you get this far, userinput must equal 'y'
    choice = ""
    display_menu()
    choice = str(input("Select a conversion (a/b): "))
    while choice == "a" or "b":
        if choice == "a":
            print()
            feet = int(input("Enter feet: "))
            meters = conversions.to_meters(feet)
            print(str(round(meters, 2)) + " meters")
            print()
            break
        elif choice == "b":
            print()
            meters = int(input("Enter Meters: "))
            feet = conversions.to_feet(meters)
            print(str(round(feet, 2)) + " feet")
            print()
            break
        elif choice != "a" or "b":
            print("Please enter a valid option a or b")
            choice = str(input("Select a conversion (a/b): "))
    userinput = input("Would you like to perform another conversion? (y/n): ").lower()
    continue

Be aware, the way you implemented the inner loop asking for the conversion type doesn't allow you to exit the xcript if you suddenly decide abort the procedure e.g. a Keyboard interrupt.

[Edit] I've not looked at your inner loop. As someone else has suggested,

while choice == "a" or "b" 

will always evaluate to True. Why? beacuse internally python will split this expression:

(choice == "a") or "b"

It doesn't matter if choice == "a", as "b" is a non-empty string and thus evaluates to True. If you were to rewrite yout inner loop as follws:

while choice: # Will evaluate to True as long as choice isn't an empty string.
    if choice == "a":
        print()
        feet = int(input("Enter feet: "))
        meters = conversions.to_meters(feet)
        print(str(round(meters, 2)) + " meters")
        print()
        break
    elif choice == "b":
        print()
        meters = int(input("Enter Meters: "))
        feet = conversions.to_feet(meters)
        print(str(round(feet, 2)) + " feet")
        print()
        break
    else:
        print("Please enter a valid option a or b")
        choice = input("Select a conversion (a/b): ")

you'll be able to give the user an option to exit the inner loop by inputing nothing and you'll remove an uneccessary double-check if the choice equals a or b.

Tip: If you want to check if one variable matches one of some different options in a while statement, you could use the following:

while var in [opt1, opt2 ...]:
    ...
David
  • 16
  • 3