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.
Vishal JoshiPosted Mar 24, 2023, 6:38 AM
Hello Valerie,
You can use the code below to get only integer numbers in python.
Or else you can use isdecimal() to check if the input is decimal. it's similar to TryParse as c#
Thanks
Valerie MeunierPosted Mar 24, 2023, 10:35 AM
Thanks
Valerie MeunierPosted Mar 23, 2023, 1:27 PM
I tried with try / except and it works, but i wonder whether it exists a specific function like TryParse...