繁体   English   中英

Python:需要关于功能的帮助

[英]Python : Need Help About functions

list = [1]
list2 =[1]

def all():
 num = 3
  def first(new_num): #new_num = new_index  = 3
   if num not in list:
    list.append(num)
    new_index = list.index(num)
    first(new_index) #new_index = num = 3
   else:
    second(new_num) #assign new_num to the function second()
  def second(item): #item = new_num in function first(new_num)
    print("hello" ,  item)
first()

全部()

最底部有一个 all() 但我不知道为什么它不在里面

这是一个范围界定问题。

function first在 function 内部,如果它是 inside all from outside all all不能first调用。 应该做的是调用all并在all中调用, first像这样启动:

list = [1]
list2 =[1]

def all():
    num = 3
    def first(new_num): #new_num = new_index  = 3
        if num not in list:
            list.append(num)
            new_index = list.index(num)
            first(new_index) #new_index = num = 3
        else:
            second(new_num) #assign new_num to the function second()
    def second(item): #item = new_num in function first(new_num)
        print("hello" ,  item)
    first(num)
all()

除此之外,您的缩进有点混乱,您应该使用 4 个空格制表符进行每个pep8的缩进。

暂无
暂无

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

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