简体   繁体   English

你如何定义一个for循环?

[英]How do you define a for loop?

How do you define a variable in a for loop?如何在 for 循环中定义变量?

def test():
    for i in range(1,999):
        do this thing blah blah blah
def another_function(parameter):
    test() # runs the function test()

But when I run the if __name__ == __main__ , it asks for me to define the variable i .但是当我运行if __name__ == __main__时,它要求我定义变量i

if __name__ == __main__:
    another_function(parameter = i # define i)

One thing I've tried is to define i using its own for loop:我尝试过的一件事是使用自己的 for 循环定义i

if __name__ == __main__:
    i = i in range(1, 999)

    another_function(parameter = i)

And that doesn't work.那是行不通的。

Should I try another way that isn't if __name__ == __main__ or is there even a way to define i ?我应该尝试另一种方法,不是if __name__ == __main__还是有办法定义i

A few basic for loop examples in python: python 中的一些基本 for 循环示例:

def for_loop_example_1():
    for i in range(0,10):
        print("Hello world")

def for_loop_example_2():
    sum = 0
    for i in range(0,10):
        sum+=i
    return sum

if __name__ =='__main__':
    for_loop_example() #this prints "Hello world" 10 times
    print("\n") #Adding a line break
    sum = for_loop_example_2
    print(sum) #This prints 45 i.e sum of numbers from 0 till 10

You are trying to pass a parameter to the function.您正在尝试将参数传递给 function。 The value of the parameter should be i .参数的值应该是i But, what is the value of i ?但是, i的值是多少? Before using i , you should specify its value.在使用i之前,您应该指定它的值。

i = i in range(1, 999) # This will only make the value of i as True and pass parameter variable with a value of True

If I understood well, here:如果我理解得很好,这里:

if __name__ == __main__:
    another_function(parameter = i # define i)

you are trying to set parameter as i , but i is a variable, so you have to create it first and allocate a value.您正在尝试将参数设置为i ,但i是一个变量,因此您必须先创建它并分配一个值。 That's way it is asking to define i .这就是它要求定义i的方式。 This would work:这会起作用:

if __name__ == __main__:
        i = 2
        another_function(parameter = i # define i)

When you try to do: i = i in range(1, 999) , I guess you are trying to create a for loop in one line, but that's wrong.当您尝试这样做时: i = i in range(1, 999) ,我猜您正试图在一行中创建一个 for 循环,但这是错误的。 You need square brackets.你需要方括号。 And calling the function inside the brackets.并在括号内调用 function。

[another_function(parameter = i) for i in range(1, 999)]

This would run the for loop, assignt i = 1, call another_function, reassign i = 2, recall the function, and so on.这将运行 for 循环,分配 i = 1,调用 another_function,重新分配 i = 2,调用 function,等等。

Since parameter isn't used, you can fix this code by removing it:由于未使用parameter ,您可以通过删除它来修复此代码:

def test():
    for i in range(1, 999):
        # do this thing blah blah blah

def another_function():
    test() # runs the function test()

if __name__ == __main__:
    another_function()

Since i is defined inside the for loop in test there is no way (and no reason) to refer to it outside of that function, and since parameter wasn't used in your original code there's no reason to need to come up with a value (like i ) to pass into it from your __main__ block.由于i是在test中的 for 循环内定义的,因此无法(也没有理由)在 function 之外引用它,并且由于原始代码中未使用parameter ,因此没有理由需要提出一个值(如i )从你的__main__块传入它。

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

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