-2

The code seems to continue running even after inputting Q or q. I know we need to break it from the loop but when I keep it inside the main function the output seems to be both "Thank you for using the program!" "Invalid Input, Please Enter 1-2-Q for the desired action to be performed" when I enter Q

def main():
    count = True
    while count == True:
        print("1 - Chemical formula for the Ionic Compound")
        print("2 - Chemical Name for the Ionic Compound")
        print("Q - EXIT")
        print("-----------------------------------------------")
        action = input("What action would you like to execute? ")
        menu(action)

def menu(action):
    if action == "1":
        print()
        e1 = input("Enter chemical symbol for first element: ")
        c1 = int(input("Enter the first element charge: "))
        e2 = input("Enter chemical symbol for second element: ")
        c2 = int(input("Enter the second element charge: "))
        print()
        print(chemistry.ionicCompound(e1, c1, e2, c2))
    elif action == "2":
        print()
        e1 = input("Enter chemical symbol for first element: ")
        e2 = input("Enter chemical symbol for second element: ")
        print(chemistry.ionicName(e1, e2))
    elif action == "Q" or action == "q":
        print()
        print("Thank you for using the program!")
    else:
        print()
        print("Invalid Input, Please Enter 1-2-Q for the desired action to be performed")
        print()
    print("----------------------------------------------------------------------------------")
main()
  • But you never break the loop. – DYZ May 19 '22 at 21:52
  • You need to do something to stop the loop if your input is `q`. How can you stop the loop? By using `break` or by falsifying the loop condition. `count== True` (side note: you don't need to do `count == True`, just `count` will suffice if `count` is a boolean). What can you do from inside the `menu` function that will give some information about whether the loop needs to be broken to a caller outside the function? – Pranav Hosangadi May 19 '22 at 21:55

1 Answers1

-1

Try this:

RUNNING = True

def main():
    count = True
    while count == True and RUNNING:
        print("1 - Chemical formula for the Ionic Compound")
        print("2 - Chemical Name for the Ionic Compound")
        print("Q - EXIT")
        print("-----------------------------------------------")
        action = input("What action would you like to execute? ")
        menu(action)

def menu(action):
    global RUNNING
    if action == "1":
        print()
        e1 = input("Enter chemical symbol for first element: ")
        c1 = int(input("Enter the first element charge: "))
        e2 = input("Enter chemical symbol for second element: ")
        c2 = int(input("Enter the second element charge: "))
        print()
        print(chemistry.ionicCompound(e1, c1, e2, c2))
    elif action == "2":
        print()
        e1 = input("Enter chemical symbol for first element: ")
        e2 = input("Enter chemical symbol for second element: ")
        print(chemistry.ionicName(e1, e2))
    elif action == "Q" or action == "q":
        print()
        print("Thank you for using the program!")
        RUNNING = False
    else:
        print()
        print("Invalid Input, Please Enter 1-2-Q for the desired action to be performed")
        print()
    print("----------------------------------------------------------------------------------")
main()
David Camp
  • 322
  • 2
  • 9