gpt4 book ai didi

python - 函数内的 Python For 循环错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:46 25 4
gpt4 key购买 nike

所以我试图更好地理解函数并使用 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'

最佳答案

两个循环同时使用两个列表,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)

The zip() function takes:

iterables - can be built-in iterables (like: list, string, dict), or user-defined iterables (object that has __iter__ method)

The zip() function returns an iterator of tuples based on the iterable object.

  • If no parameters are passed, zip() returns an empty iterator
  • If a single iterable is passed, zip() returns an iterator of 1-tuples. Meaning, the number of elements in each tuple is 1.
  • If multiple iterables are passed, ith tuple contains ith Suppose, two iterables are passed; one iterable containing 3 and other containing 5 elements. Then, the returned iterator has 3 tuples. It's because iterator stops when shortest iterable is exhaused.

关于python - 函数内的 Python For 循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58570229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com