Home > Software design >  How to run a loop and conditional based on data types in python?
How to run a loop and conditional based on data types in python?

Time:12-22

How to check a data type in loop and based on the data type run a conditional or loop?

For an example, I want to take an input from the user. Suppose that is a string or an integer, if the input is an integer then while loop will be executed else not be executed.

CodePudding user response:

try:
    input = int(input)
    flag = True
except:
    flag = False

while flag:
    pass

CodePudding user response:

You can also try isnumeric() of strings:

i=input()
if i.isnumeric():
    #while:
        #some condition
else:
    #some condition
  • Related