Python Forum

Full Version: User Defined function not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

iam very beginner to python. my query is, i wrote user defined function(num()) to take input value (list item). if input value is not in the list item then again i want to ask input value (i.e. Again i want to call udf(inputnumber). While executing the code when i calling the function, code showing no error but executing nothing.

def num():
    n = int(input("Enter a list item: "))    
num()

a=[56,34,29,39]
if n in a :
    print("Given number index in the list is : ", a.index(n))
else:
    print("input value is not in the list item")
    num()
Ex: if input value is 30 then code should call function(num) and again ask input value till input value in the list.
a=[56,34,29,39]

def num():
    n = int(input("Enter a list item: "))
    
    if n in a:
        print("Given number index in the list is : ", a.index(n))
    else:
        print("input value is not in the list item")
        num()
num()
Hi Axel, Thank you for reply.

i want to create udf to ask input value (function contains input statement only). remaining part should not include in function. if input value not in the list then only we call the function.

(Dec-17-2019, 12:10 PM)Axel_Erfurt Wrote: [ -> ]
a=[56,34,29,39]

def num():
    n = int(input("Enter a list item: "))
    
    if n in a:
        print("Given number index in the list is : ", a.index(n))
    else:
        print("input value is not in the list item")
        num()
num()
Modifying Axel's code to meet the new specification:
a=[56,34,29,39]
 
def num():
    n = int(input("Enter a list item: "))
    return n

n=num()

while n not in a:
    print("input value is not in the list item")
    n=num()
    
print("Given number index in the list is : ", a.index(n))
please, avoid these nested calls to function. and make your function to return something. and take the list as an argument to function...