我想要用戶輸入信息並打印出總列表。但是,當用戶輸入另一個列表時,它只會打印出第一個列表。我怎樣才能讓程序打印用戶的總輸入。這是我的代碼。允許用戶在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