Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 82.4k

how to check input in a loop

Mar 23 2023 11:57 AM

Hi

I want to check the input of a variable before going further in the program. I can do it in C# like this:

int= pr=0;
do
  Console.Write("Price: ");
while (float.TryParse(Console.ReadLine(), out pr) == false);

As long as the input is not ok, the loop will still run. Never an error is shown.

But how to do that with python? I tried this, but i get always an error when the unput is bad.

while True:
    pr=int(input("Price: "))
    if type(pr) == int:
        break
    else:
        print("No int")
 

Thanks.

V.


Answers (3)