'How to stop the loop when "Q" or "q" is entered [duplicate]
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()
Solution 1:[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()
Solution 2:[2]
The problem occurs when Django tries to serialize your Model for the purpose of creating the migration file.
If you look inside django/db/migrations/serializer.py you'll see the following:
if '<' not in self.value.__qualname__: # Qualname can include <locals>
return '%s.%s' % (module_name, self.value.__qualname__), {'import %s' % self.value.__module__}
raise ValueError(
'Could not find function %s in %s.\n' % (self.value.__name__, module_name)
)
Meaning that local functions cannot be used.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Camp |
| Solution 2 | Ari |
