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 :
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).