I want to check the input of the user.
Only positive nos are allowed. I want to pass the input of the user to a function which checks that if it is i positive no. then it allows the user to continue further otherwise it gives an error and asks the user if he would like to try again. if the user enters yes the function should go back to my calling fucntion to ask the user a new input and if user enters no the program terminates.
how do i do it?
I know how to chek but m facing problem in going back to the calling statement n terminating the program.

the calling statement is in the main part and i not a separate function

Are you usng Python2 or Python3?
Are your positive numbers integers or floats?

Are you usng Python2 or Python3?
Are your positive numbers integers or floats?

m using python version 2.6.2

actually i have to check whether the input is an integer or not. by mistake i wrote positive nos.
whether the no. is integer or not can be checked by type(x) func but i dont know how to proceed further

Well you can use the type() function to show what type the number is:

>>> type(12)
<type 'int'>
>>> type(12.4)
<type 'float'>
>>> type(input())
12
<type 'int'>
>>> type(input())
12.5
<type 'float'>
>>>

So you can adapt that to check for any type. So when you input something you can do something like

#Keep pass as False until they have entered an integer :)
cont = False

while not cont:
    i = input("Enter input (integers only):")

   #This checks if the type of the input is equal
   # to the int type. Just as we saw above
    if type(i) == int:
        #If it is then the loop can stop and the program can continue
        cont = True

Hope that helps :)

Another way to solve this is to use string function isdigit() ...

while True:
    num = raw_input("Enter an integer: ")
    if num.isdigit():
        num = int(num)
        break
    print "Try again, value you entered was not an integer."

print(num)

Another way to solve this is to use string function isdigit() ...

while True:
    num = raw_input("Enter an integer: ")
    if num.isdigit():
        num = int(num)
        break
    print "Try again, value you entered was not an integer."

print(num)

Its not accepting negative integers!!! how do i modify it so tht it works wid negative integers as well

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.