简体   繁体   English

函数全局/局部变量更新问题

[英]function global/local variable update issues

Can someone point out why this function is not outputting?有人能指出为什么这个函数没有输出吗? Please explain and give examples.请解释并举例说明。 I am just unable to figure out the issue.我只是无法弄清楚这个问题。

def double(lst):

    count=0
    result='Found'
    lsy=[count,result]

    for i in lst:
        word=i
        if 'mm' in word:
            result='Found'
            count=0
            break
        if 'nn' in word:
            result='None'
            count+=1

    return lsy

double(['Ammy','Timmy','Jimmy'])
result = double(['Ammy','Timmy','Jimmy'])
print(result)
# [0, 'Found']

use print() on your function call.在函数调用中使用print() Otherwise nothing is given to stdout, hence why you see nothing.否则什么都没有给 stdout,因此你什么也看不到。

If you meant that the function is producing a wrong result then the problem lies in lsy because you do not update it with the result after you process the result inside the for loop.如果你指的是功能产生错误的结果,那么问题就出在lsy因为你不与更新result ,你处理后result里面的for循环。

If you really meant that you are not getting the output displayed then simply print the result:如果您的意思是没有显示输出,则只需打印结果:

print(double(['Ammy', 'Timmy', 'Jimmy']))

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

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