0

这是一个比大多数问题更容易的问题。我是编程领域的新手,可以使用一些帮助。我正在尝试将下面的文本打印到用户指定的名称列表中。我得到的错误:

TypeError: GoodDaySir() takes 0 positional arguments but 1 was given

蟒蛇3.3

mylist=input('Who is visiting today?')
def GoodDaySir():
    for i in range(len(mylist)):
        print('Hello to you.')
        print('Have a good day, ', mylist)
4

3 回答 3

1

def您忘记在and之后缩进行for

mylist=input('Who is visiting today?')
def GoodDaySir():
    for i in range(len(mylist)):
         print('Hello to you.')
         print('Have a good day, ', mylist)

虽然我不知道你想用这段代码完成什么;-)

你可能想要这样的东西:

def GoodDaySir(visitors):
    """ This function takes a list() of visitors and prints out a greeting
        for each person.
    """
    for person in visitors :
         print('Hello to you.')
         print('Have a good day, ', person)

# Get a list of names separated by ","
visitors = raw_input('Who is visiting today?')

# Convert the string to an actual list()
visitorsList = [x.strip() for x in visitors.split(',')]

# Call our function with the list as parameter and greet the visitors
GoodDaySir(visitorsList)
于 2013-04-30T18:45:46.213 回答
0
TypeError: GoodDaySir() takes 0 positional arguments but 1 was given

此错误意味着您将参数传递给不带参数的函数。你已经定义了

def GoodDaySir():

没有参数。当你传递mylistGoodDaySir它时不知道如何处理它。也许将您的功能更改为

def good_day_sir(people):
    for person in people:
        print('Hello to you.')
        print('Have a good day, ', person)
于 2013-04-30T20:49:35.527 回答
-1

主要问题是您根本没有使用缩进,但它仍然无法正常工作。

解决方案的一个例子可能是:

def GoodDaySir():
    names = raw_input('Who is visiting today?') #this returns a string of names 
    namesList = names.split(',') #let's suppose that those names are separated by commas

    for name in namesList:
        print('Hello to you.')
        print('Have a good day, ', name)`
于 2013-04-30T19:19:47.280 回答