这是一个比较容易的问题。我是编程领域的新手,可以使用一些帮助。我正在尝试将下面的文本打印到用户指定的名称列表中。我得到的错误:
TypeError: GoodDaySir() takes 0 positional arguments but 1 was given
python 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)
答案 0 :(得分:1)
您忘记在def
和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)
答案 1 :(得分:0)
TypeError: GoodDaySir() takes 0 positional arguments but 1 was given
此错误表示您将参数传递给不带参数的函数。你已经定义了
def GoodDaySir():
没有参数。当您将mylist
传递给GoodDaySir
时,它不知道如何处理它。也许将你的功能改为
def good_day_sir(people):
for person in people:
print('Hello to you.')
print('Have a good day, ', person)
答案 2 :(得分:-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)`