函数内的Python For循环错误

时间:2019-10-26 11:12:30

标签: python

因此,我试图更好地理解函数,并使用for循环从多个列表中进行选择。这是我创建的基本功能,并且可以正常工作:

def my_function(person, feeling):
    print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling))

my_function('Matthew', 'rejuvinated')

但是,为了更进一步,我想从它们各自的列表中选择一个名称和一种感觉,并将其插入到新函数中。当我尝试以下操作时,出现错误。任何帮助将不胜感激!

people = ['Colby', 'Hattie', 'Matthew', 'Stephen', 'Lee', 'Deb', 'Sharon', 'Pete']
feelings = ['happy', 'sad', 'cold', 'cranky', 'happy', 'successful', 'spunky', 'warm', 'nerdy']

def my_function(person, feeling): 
    """This function produces a statement which inserts a name and a feeling"""
    for p in enumerate(people):
            person = p
    for f in enumerate(feelings):
            feeling = f
    print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling)) 
    return my_function()

my_function(people, feelings)

Hello, (7, 'Pete'), how are you? It seems that you are feeling (8, 'nerdy')!
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-2b2a265c427c> in <module>
     11     return my_function()
     12 
---> 13 my_function(people, feelings)

<ipython-input-1-2b2a265c427c> in my_function(person, feeling)
      9             feeling = f
     10     print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling))
---> 11     return my_function()
     12 
     13 my_function(people, feelings)

TypeError: my_function() missing 2 required positional arguments: 'person' and 'feeling'

3 个答案:

答案 0 :(得分:1)

两次同时使用两个循环zip

peoples = ['Colby', 'Hattie', 'Matthew', 'Stephen', 'Lee', 'Deb', 'Sharon', 'Pete']
feelings = ['happy', 'sad', 'cold', 'cranky', 'happy', 'successful', 'spunky', 'warm', 'nerdy']

def my_function(persons, feelings):
    """This function produces a statement which inserts a name and a feeling"""
    # python provide a zip built-in function to loop over multiple list in the same time
    for person, feeling in zip(persons, feelings):
        # print the message for every element
        print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling))

my_function(peoples, feelings)
  

zip()函数需要:

     

可迭代项-可以内置iterables(例如:list, string, dict),或者   用户定义的可迭代对象(具有__iter__方法的对象)

     

zip()函数基于以下内容返回一个元组迭代器:   iterable个对象。

     
      
  • 如果未传递任何参数,则zip()返回一个空的迭代器
  •   
  • 如果传递了单个Iterable,则zip()返回一个1元组的迭代器。意思是,每个元组中的元素数为1。
  •   
  • 如果传递了多个可迭代对象,则ith元组包含ith,假设有两个可迭代对象被传递;一个可迭代的包含3个,另一个   包含5个元素。然后,返回的迭代器具有3个元组。它的   因为迭代器在最短的可迭代数用尽时停止。
  •   

答案 1 :(得分:1)

这是您的职能以及人和感情的清单。

def my_function(person, feeling):
    print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling))

people = ['Colby', 'Hattie', 'Matthew', 'Stephen', 'Lee', 'Deb', 'Sharon', 'Pete']
feelings = ['happy', 'sad', 'cold', 'cranky', 'happy', 'successful', 'spunky', 'warm', 'nerdy']

您需要一个人和一种感觉来调用该函数。要获得所有组合,您必须遍历列表。

for person in people:
    for feeling in feelings:
        my_function(person, feeling)

如果您只希望一个随机的人有一个随机的感觉,则可以使用random.choice

import random
person = random.choice(people)
feeling = random.choice(feelings)
my_function(person, feeling)

随意组合这些解决方案。遍历所有名称,让每个人都有一种随意的感觉。

import random
for person in people:
    feeling = random.choice(feelings)
    my_function(person, feeling)

关于您使用enumerate

for person in enumerate(people):
    print(person)

enumerate将为您提供索引和元素的元组。最终的价值是您在这里不需要的任何东西。

(0, 'Colby')
(1, 'Hattie')
(2, 'Matthew')
(3, 'Stephen')
(4, 'Lee')
(5, 'Deb')
(6, 'Sharon')
(7, 'Pete')

答案 2 :(得分:1)

那又怎么样:

def my_function(person, feeling):
    print(f'Hello, {person}, how are you?'\
          f'It seems that you are feeling {feeling}!')

my_function('Matthew', 'rejuvinated')


def my_function_2(people, feelings, fun): 
    for i, person in enumerate(people):
        fun(person, feelings[i])


people = ['Colby', 'Hattie', 'Matthew', 'Stephen', 'Lee', 'Deb', 'Sharon', 'Pete']
feelings = ['happy', 'sad', 'cold', 'cranky', 'happy', 'successful', 'spunky', 'warm', 'nerdy']
my_function_2(people, feelings, my_function)


Hello, Matthew, how are you?It seems that you are feeling rejuvinated!
Hello, Colby, how are you?It seems that you are feeling happy!
Hello, Hattie, how are you?It seems that you are feeling sad!
Hello, Matthew, how are you?It seems that you are feeling cold!
Hello, Stephen, how are you?It seems that you are feeling cranky!
Hello, Lee, how are you?It seems that you are feeling happy!
Hello, Deb, how are you?It seems that you are feeling successful!
Hello, Sharon, how are you?It seems that you are feeling spunky!
Hello, Pete, how are you?It seems that you are feeling warm!