简体   繁体   English

在for循环中使用函数时遇到麻烦

[英]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) 上面的代码的问题是,它没有执行函数f(x)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM