Python 在类中创建while循环并将用户输入添加到列表中?
*****更新:多亏了帮助,在循环过程中找到了答案,正在努力打印带有用户在列表中输入的动物类型和名称的列表******Python 在类中创建while循环并将用户输入添加到列表中?,python,class,input,while-loop,Python,Class,Input,While Loop,*****更新:多亏了帮助,在循环过程中找到了答案,正在努力打印带有用户在列表中输入的动物类型和名称的列表****** # Define your pet Class # Setup attributes for the type of animal and a name # Include type of animal and name in the __init__ method class petClass: def __init__(self, animal, name):
# Define your pet Class
# Setup attributes for the type of animal and a name
# Include type of animal and name in the __init__ method
class petClass:
def __init__(self, animal, name):
self.animal = animal
self.name = name
# Create an empty List to store your pet objects
myPetObjects = []
# Create a While loop that continues to ask the user for the information for a new pet object
# Within the loop, create new pet objects with the inputted name and animal type.
# Add this new pet to our pet List
# Ask if the user wants to add more. If not, exit the While loop
while True:
new_pet = petClass(input("what type of animal"), input("what is its name"))
myPetObjects.append(new_pet)
response = input("add another animal?")
if response == 'no':
break
# Create a new For loop that goes through your pet List.
# Print out the animal type and name of the pets within your List
for pet in myPetObjects:
print(myPetObjects)
打印列表的输出不是我想要的,只是寻找名称和动物类型 下面是我注意到的代码 首先,你要说:
if input = no:
应改为
if response == 'no':
我将您的名字“input”改为“response”,因为input是python中的内置函数,我们不应该将其用作变量名
==运算符检查其操作数的等效性,在本例中为输入和“否”。single equals=是python中的赋值运算符,这意味着它将=右侧的值存储到左侧给定的名称中。比如说,
myPetObjects = []
将右侧的空列表指定给左侧的名称myPetObjects
在我重写的代码行中,您可能会注意到代码中的另一个变化是添加到“否”的引号。这将比较名称“input”引用的值与字符串文本“no”。如前所述,不带引号的“no”指的是尚未定义的名称/变量no,这会使Python感到困惑
另一件事:因为您使用的是break语句,一旦运行就会离开循环,所以可以更新while循环行,只需说
while True:
这样,循环将无限期地继续,直到使用“break”语句(您已经包含)显式退出
至于while循环的实际主体,它需要一些重构。声明
input("what type of animal")
是对python内置输入函数的调用,当提示“what type of animal”时,它将计算或返回用户输入的字符串
所以,我们可以把它当作一个字符串。
记住这一点,让我们创建您的宠物对象。
在Python中创建对象具有通用语法
object_name = class_name(parameters)
其中参数指的是uu init_uuu所采用的参数。
这将通过使用赋值运算符=,将类的对象实例class_name分配给对象_name
要将新对象附加到myPetObjects,我们不需要为新对象指定名称。我们可以这样做:
myPetObjects.append(class_name(parameters))
在您的情况下,可以这样写:
myPetObjects.append(petClass(input("what type of animal"), input("what is its name")))
所以,我们的整个循环:
while True:
myPetObjects.append(petClass(input("what type of animal"), input("what is its name")))
response = input("add another animal?")
if response == 'no':
break
也许是更容易阅读的版本:
while True:
new_pet = petClass(input("what type of animal"), input("what is its name"))
myPetObjects.append(new_pet)
response = input("add another animal?")
if response == 'no':
break
我希望这能有所帮助,如果您还有任何问题,如果有什么不清楚的地方,或者如果我没有按照您想要的方式回答问题,请告诉我。祝你好运 下面是我注意到的代码 首先,你要说:
if input = no:
应改为
if response == 'no':
我将您的名字“input”改为“response”,因为input是python中的内置函数,我们不应该将其用作变量名
==运算符检查其操作数的等效性,在本例中为输入和“否”。single equals=是python中的赋值运算符,这意味着它将=右侧的值存储到左侧给定的名称中。比如说,
myPetObjects = []
将右侧的空列表指定给左侧的名称myPetObjects
在我重写的代码行中,您可能会注意到代码中的另一个变化是添加到“否”的引号。这将比较名称“input”引用的值与字符串文本“no”。如前所述,不带引号的“no”指的是尚未定义的名称/变量no,这会使Python感到困惑
另一件事:因为您使用的是break语句,一旦运行就会离开循环,所以可以更新while循环行,只需说
while True:
这样,循环将无限期地继续,直到使用“break”语句(您已经包含)显式退出
至于while循环的实际主体,它需要一些重构。声明
input("what type of animal")
是对python内置输入函数的调用,当提示“what type of animal”时,它将计算或返回用户输入的字符串
所以,我们可以把它当作一个字符串。
记住这一点,让我们创建您的宠物对象。
在Python中创建对象具有通用语法
object_name = class_name(parameters)
其中参数指的是uu init_uuu所采用的参数。
这将通过使用赋值运算符=,将类的对象实例class_name分配给对象_name
要将新对象附加到myPetObjects,我们不需要为新对象指定名称。我们可以这样做:
myPetObjects.append(class_name(parameters))
在您的情况下,可以这样写:
myPetObjects.append(petClass(input("what type of animal"), input("what is its name")))
所以,我们的整个循环:
while True:
myPetObjects.append(petClass(input("what type of animal"), input("what is its name")))
response = input("add another animal?")
if response == 'no':
break
也许是更容易阅读的版本:
while True:
new_pet = petClass(input("what type of animal"), input("what is its name"))
myPetObjects.append(new_pet)
response = input("add another animal?")
if response == 'no':
break
我希望这能有所帮助,如果您还有任何问题,如果有什么不清楚的地方,或者如果我没有按照您想要的方式回答问题,请告诉我。祝你好运 通过为while条件提供true或false,您可以控制迭代:
#prompting user to enter inputs to the object
while answer != 'no':
animalInput = input('What type of animal: ')
nameInput = input('what is its name: ')
#creating instances and adding to the list
myPetObjects.append(petClass(animalInput, nameInput))
#Adding more pets into object
answer = input("Do you want to add more(yes/no): ")
if answer == "yes":
# Do this again
elif answer == "no":
break
#print all
for pet in myPetObjects:
print(pet.animal +":"+ pet.name)
通过为while条件提供true或false,您可以控制迭代:
#prompting user to enter inputs to the object
while answer != 'no':
animalInput = input('What type of animal: ')
nameInput = input('what is its name: ')
#creating instances and adding to the list
myPetObjects.append(petClass(animalInput, nameInput))
#Adding more pets into object
answer = input("Do you want to add more(yes/no): ")
if answer == "yes":
# Do this again
elif answer == "no":
break
#print all
for pet in myPetObjects:
print(pet.animal +":"+ pet.name)
如果你有条件打破它,你可以简单地使用
while True
。不需要用“已解决”或更新:这对你有帮助。记住,这个问答不是为你一个人准备的。其他人可能会遇到相同的问题,他们可以从接受的或投票率较高的答案中获得解决方案。如果您有条件打破此问题,您可以简单地使用while True
。无需使用“已解决”或更新:这对你有帮助。记住,这个问答不是为你一个人准备的。其他人可能会遇到同样的问题,他们可以从被接受的或投票率很高的答案中得到解决方案。这比我想象的要有用得多!非常感谢。我唯一有问题的是如何打印列表,以读取我输入的动物类型和名称。我记得在我的课堂上,当与班级一起工作时,列表不会打印相同的内容?哦,是的!我忘了那部分。要打印对象的属性,请执行打印(object.attribute)。在本例中,您可以通过访问列表的成员来访问对象。我会把密码贴出来