Python Forum
User Defined function not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User Defined function not working
#1
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.
Reply
#2
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()
Reply
#3
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()
Reply
#4
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))
Reply
#5
please, avoid these nested calls to function. and make your function to return something. and take the list as an argument to function...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 512 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,165 Sep-03-2023, 03:22 PM
Last Post: deanhystad
Shocked kindly support with this dropna function not working gheevarpaulosejobs 2 613 Jul-24-2023, 03:41 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Getting NameError for a function that is defined JonWayn 2 1,056 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,773 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,236 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Exclamation Function Not Working Alivegamer 7 1,817 Jul-19-2022, 01:03 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,589 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Multiple user defined plots with secondary axes using for loop maltp 1 1,393 Apr-30-2022, 10:19 AM
Last Post: maltp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020