简体   繁体   中英

Python loop to create objects from class untill user says to stop

I am trying to create several objects from a class that the user can create by inputting data.I have created an example code below to illustrate what I want to achieve.

class Person:
def __init__(self, Name, Age, City):
    self.Name = Name
    self.Age = Age
    self.City = City

Person1 = Person(str(input("Name ")), str(input("Age ")), str(input("City ")))
Person2 = Person(str(input("Name ")), str(input("Age ")), str(input("City ")))
Person3 = Person(str(input("Name ")), str(input("Age ")), str(input("City ")))
Persons = (Person1, Person2, Person3)


for person in Persons:
    print("Hello " + person.Name)
    print("You are " + person.Age)
    print("And live in " + person.City)

Where Person1, 2 and 3 are I actually want python to ask the user to input this data, untill the person does not want to input any data anymore.

Currently I am using the following code to try to accomplish this:

class Person:
    def __init__(self, Name, Age, City):
        self.Name = Name
        self.Age = Age
        self.City = City

cont = True
entity = {str("Person" + 1)}
while True:
    entity = Person(str(input("Name ")), str(input("Age ")), str(input("City ")))
    cont = input("Continue? True of False ")

Persons = (entity)


for person in Persons:
    print("Hello " + person.Name)
    print("You are " + person.Age)
    print("And live in " + person.City)

I figured a while loop would do the trick here where the user can say True or False to let the while loop continue untill the user types False, but it doesn't seem to do it and I am having issues trying to figure out how to accomplish this. Python gives me an error: can only concetenate str (not "int") to str, but I am sure that there are more issues here than that. Can someone help me please?

Thanks!

Someone posted this but that comment got deleted for some reason. Anyway this seems to work out.

 # class definition unchanged
class Person:
    def __init__(self, Name, Age, City):
        self.Name = Name
        self.Age = Age
        self.City = City

# using a list, because it's mutable
Persons = []

while True:
    entity = Person(str(input("Name ")), str(input("Age ")), str(input("City ")))

    # a different option for breaking the loop, commented out
    #if entity.Name == "":
    #    break

    # add the new person to the list
    Persons.append(entity)

    cont = input("Continue? Press Enter. Else F ")

    # check the input to decide whether to continue
    if cont == "False" or cont == "F" or cont == "f":
        break;

# output loop unchanged
for person in Persons:
    print("Hello " + person.Name)
    print("You are " + person.Age)
    print("And live in " + person.City)

Thank you all for helping me out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM