Search code examples
pythonarrayspython-3.xfor-loopdefinition

Having trouble with using functions in for loop


X=[1,2,1,0,1]
def f(x):
     return {1:'car',2:'bike',0:'bus'}.get(x,'default')

for i in X:
    print(i)
    f(i)
output: 1 2 1 0 1 

The problem with the above code is, its not executing the function f(x)


Solution

  • What about this? It proves that it's executing normally

    X=[1,2,1,0,1]
    def f(x):
         return {1:'car',2:'bike',0:'bus'}.get(x,'default')
    
    for i in X:
        print(i)
        print(f(i))