Python Forum
User Defined function not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: User Defined function not working (/thread-23224.html)



User Defined function not working - Raj_Kumar - Dec-17-2019

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.


RE: User Defined function not working - Axel_Erfurt - Dec-17-2019

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()



RE: User Defined function not working - Raj_Kumar - Dec-17-2019

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()



RE: User Defined function not working - jefsummers - Dec-17-2019

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))



RE: User Defined function not working - buran - Dec-17-2019

please, avoid these nested calls to function. and make your function to return something. and take the list as an argument to function...