Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i get the while true loop to keep running when anything other than (P,p,d,d) is entered. Current it just repeats if (P,p,d,d)

How do i get the while true loop to keep running when anything other than (P,p,d,d) is entered. Current it only repeats if (P,p,d,d) is entered. If i enter otherwise it prints "Invalid" and moves on to the next loop.

while True:
    print("How did you acquire the flat ")
    source = input("(Purchased (P) || Produced (D) ) : ")
    if ((source != 'P') and (source != 'p') and (source != 'd') and (source != 'D')):
        print("\n Invalid Code\n")
        break;
  
 
while True:
    print("\nPlease Select the type of egg ")
    type = input("\n (W)et \n (B)ig \n (F)ree  \n (O)ld  : ")
    if (type != 'W' and type != 'B' and type != 'F' and type != 'O' and type != 'w' and type != 'b' and type != 'f' and type != 'o'):
        print("\n Invalid Code\n")
        break;
           

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The while loop should only break if the input is a valid code, and continue (after printing the error message) otherwise. You can also simplify your if statement conditions WLOG.

while True:
    print("How did you acquire the flat ")
    source = input("(Purchased (P) || Produced (D) ) : ")
    if source.lower() in ['p', 'd']:
        break

    print("\n Invalid Code\n")

   
while True:
    print("\nPlease Select the type of egg ")
    type = input("\n (W)et \n (B)ig \n (F)ree  \n (O)ld  : ")
    if type.lower() in ['w', 'b', 'f', 'o']:
        break

    print("\n Invalid Code\n")

Lastly, you should avoid using names from the built-in namespace as variable names (type is a built-in function).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading