我想要用户输入信息并打印出总列表。但是,当用户输入另一个列表时,它只会打印出第一个列表。我怎样才能让程序打印用户的总输入。这是我的代码。允许用户在Python中创建多个列表
listing = []
class Car:
def __init__(self, ownerName=None, model=None, make=None, price=None):
self.ownerName = ownerName
self.model = model
self.make = make
self.price = price
def input(self):
print "Please update car info \n"
while True:
i = 0
listing.append(Car(raw_input("Owner Name"), raw_input("Model?"), raw_input("Make?"), raw_input("Price?")))
print "Updated"
print listing[i].ownerName, listing[i].model, listing[i].make, listing[i].price
addOn = raw_input("Continue? (Y/N)")
if addOn.lower() == "y":
i += 1
continue
else:
break
# search a car and print its information. Exit when user input is 'exit'
def menu():
x = Car()
print "PLease choose an option (1-4):\n"
choice = raw_input("1) input\n" \
"2) change price and owner\n" \
"3) search a car and print info\n" \
"\"exit\" Exit")
if choice == "1":
x.input()
elif choice == "2":
print "Price"
elif choice == "3":
print "Search and Print info"
menu()
如何一次打印出总列表而不是一张? – Maxwell
@Maxwell:你想在哪里打印?在收集完所有输入之后,或者是否希望在每次输入后打印整个列表? – mhawke
@Maxwell:我已经更新了答案,以显示如何在输入后打印所有车辆。那是你想要的吗? – mhawke