简体   繁体   English

Python:在ForLoop内部调用函数

[英]Python: Calling Functions Inside of a ForLoop

I'm supposed to answer specific questions given 2 different lists, a list of numbers and a list of words, using functions to answer said questions. 我应该给定2个不同的列表,一个数字列表和一个单词列表,并使用函数回答上述问题来回答特定的问题。

I currently am running into two problems. 我目前遇到两个问题。 Regarding the numbers list, I have to find the length of the list, even numbers, which numbers are greater than 100, and add 30 to each number in the list and print the answers out. 关于数字列表,我必须找到列表的长度,甚至是大于100的数字,然后将30加到列表中的每个数字并打印出答案。

First, I have these functions to answer those questions: 首先,我具有以下功能来回答这些问题:

def print_list(*args):
    for item in args:
        print item

def lenList(listName):
    return len(listName)

def numEven(num):
    if num%2==0:
        print "%d is even." % num

def moreThan100(num):
    if num>100:
        print "%d is greater than 100." % num

def adding30(num):
    print "If you add 30 to %d, you get: %d" % (num,num+30)

Then I have these functions nested inside one function: 然后,我将这些函数嵌套在一个函数中:

def leWork():
    numbers=[56,3,9,1002,33,66,789,9001,999,222,82,71,5,3]

    numLength=lenList(numbers)
    print "The length of the numbers[] is %d" % numLength

    for num in numbers:
        numEven(num), 
        moreThan100(num), 
        adding30(num)
leWork()

My problem is when running the code, I don't get an organized list stating the even numbers first,then the numbers > 100, and then the returns after adding 30. They are just scattered like this: 我的问题是在运行代码时,我没有得到一个组织好的列表,首先说明偶数,然后说明数字> 100,然后再加上30后返回。它们只是像这样散布:

The length of the numbers[] is 14
56 is even.
If you add 30 to 56, you get: 86
If you add 30 to 3, you get: 33
If you add 30 to 9, you get: 39
1002 is even.
1002 is greater than 100.
If you add 30 to 1002, you get: 1032
If you add 30 to 33, you get: 63
66 is even.
If you add 30 to 66, you get: 96
789 is greater than 100.
If you add 30 to 789, you get: 819
9001 is greater than 100.
If you add 30 to 9001, you get: 9031
999 is greater than 100.
If you add 30 to 999, you get: 1029
222 is even.
222 is greater than 100.
If you add 30 to 222, you get: 252
82 is even.
If you add 30 to 82, you get: 112
If you add 30 to 71, you get: 101
If you add 30 to 5, you get: 35
If you add 30 to 3, you get: 33

How exactly would I go about making them in a more organized list? 我将如何将它们整理成更有条理的清单?

If you want to first state all the even numbers, then state all the numbers > 100, etc., the easiest solution is to first loop and find all the even numbers, then loop and find the big numbers, etc. Like this: 如果要先声明所有偶数,然后声明所有> 100等,那么最简单的解决方案是先循环并找到所有偶数,然后再循环并找到大数,依此类推:

for num in numbers:
    numEven(num), 
for num in numbers:
    moreThan100(num), 
for num in numbers:
    adding30(num)

However, a much better solution would be to change your functions to return a value instead of just printing something, and store those return values, and then you'd be able to print them out any way you want—say, by using that print_list function you went out of your way to write. 但是,更好的解决方案是将函数更改为返回值,而不是仅打印某些内容,然后存储这些返回值,然后便可以将其打印为所需的任何方式,例如,使用该print_list函数使您无法编写代码。 For example: 例如:

def numEven(num):
    return num%2==0

def moreThan100(num):
    return num>100

def adding30(num):
    return num+30

evens, bigs, plus30s = [], [], []
for num in numbers:
    if numEven(num):
        evens.append("%d is even." % num)
    if moreThan100(num):
        bigs.append("%d is greater than 100." % num)
    plus30 = adding30(num)
    plus30s.append("If you add 30 to %d, you get: %d" % (num,plus30))

print_list(*events)
print_list(*bigs)
print_list(*plus30s)

It's how you are iterating your number, to just "fix" the error: 这就是您迭代号码的方式,只需“修复”错误即可:

for num in numbers:
    numEven(num)

for num in numbers:
    moreThan100(num)

for num in numbers:
    adding30(num)

You are iterating over the list of numbers, and for each number you are calling all your functions. 您正在遍历数字列表,并为每个数字调用所有函数。 To obtain what you want you may want consider doing 要获得您想要的东西,您可能需要考虑做

numbers=[56,3,9,1002,33,66,789,9001,999,222,82,71,5,3]
func_list = [numEven,moreThan100, adding30]

for f in func_list:
    map(f,numbers)

I would not put any print statements in your functions. 我不会在您的函数中放置任何打印语句。 Rather, all of your functions could return their numbers, then have a final function that is responsible for all of the printing. 相反,您的所有函数都可以返回其编号,然后具有一个负责所有打印的最终函数。 You could then customize your printing all in this final function, and that will be less cumbersome. 然后,您可以在此最终功能中自定义所有打印内容,这将减少麻烦。

Can you elaborate on what you mean by more organized? 您能否详细说明更有组织的意思?

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

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