2016-02-12 63 views
0

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

回答

0

那是因爲你對你的while循環的每個迭代復位i。前

i = 0 

到線while True:

這應該可以解決眼前的問題,但是,你的代碼使用了一個不尋常的設計:

招行。您不應創建Car對象,以創建Car的更多實例,然後將其插入全局列表中。

至少可以使input()爲靜態方法,並讓它返回一個Car實例列表給調用者。然後,您可以取消全局變量listing。此外,你實際上並不需要保留一個計數器在i你可以使用-1作爲下標來訪問列表中的最後一個項目:

@staticmethod 
    def input(listing=None): 
     if listing is None: 
      listing = [] 

     print "Please update car info \n" 
     while True: 
      listing.append(Car(raw_input("Owner Name"), raw_input("Model?"), raw_input("Make?"), raw_input("Price?"))) 
      print "Updated" 
      print('{0.ownerName} {0.model} {0.make} {0.price}'.format(listing[-1])) 
      addOn = raw_input("Continue? (Y/N)") 
      if addOn.lower() != "y": 
       break 

     return listing 

使用靜態方法是比較好的,因爲input()有關Car對象,因此將該函數與類一起打包是有意義的。

現在,您可以在不創建Car的實例的情況下致電input()。在您的menu()函數中刪除x = Car()並將x.input()更改爲listing = Car.input()。或者,如果您想添加到現有的「列表」列表中,請致電Car.input(listing),它將在listing附加新輸入。然後,您可以打印返回列表以查看所有用戶輸入:

def menu(): 
    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": 
     listing = Car.input() 
     # print out all user entered cars 
     for car in listing: 
      print('{0.ownerName} {0.model} {0.make} {0.price}'.format(car)) 
    elif choice == "2": 
     print "Price" 
    elif choice == "3": 
     print "Search and Print info" 
+0

如何一次打印出總列表而不是一張? – Maxwell

+0

@Maxwell:你想在哪裏打印?在收集完所有輸入之後,或者是否希望在每次輸入後打印整個列表? – mhawke

+0

@Maxwell:我已經更新了答案,以顯示如何在輸入後打印所有車輛。那是你想要的嗎? – mhawke

0

@ mhawke的答案應該可以解決您的問題。但是,我不喜歡從其中一個函數創建類的對象的想法。檢查下面編輯的代碼。

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_car(): 
    print "Please update car info \n" 
    i = 0 
    while True: 
     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": 
     input_car() 
    elif choice == "2": 
     print "Price" 
    elif choice == "3": 
     print "Search and Print info" 


menu() 
0

我清理了一下代碼。我應該現在工作。選項3爲您提供了迄今爲止所有汽車的完整列表,因此您有一個建立在其上的示例。

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 

    #to have a useful method for our example I overwrite the __str__ method from object 
    def __str__(self): 
     return ",".join([self.ownerName, self.model, self.make, self.price]) 


#input does not handle aspects of car, therefore it should be not a method of car 
def input(): 
    print "Please update car info \n" 
    while True: 
     # there is no need for 'i' so I removed it 
     car = Car(raw_input("Owner Name"), 
        raw_input("Model?"), 
        raw_input("Make?"), 
        raw_input("Price?")) 
     listing.append(car) 
     print "Updated" 
     print car #possible since __str__ is overwritten 
     addOn = raw_input("Continue? (Y/N)") 
     if addOn.lower() == "n": 
      break 

def menu(): 
    keep_running = True 
    #added a while loop so the user stays in the program until he types 'exit' 
    #changed option '3' to have a working example to build on 
    while keep_running: 
     print "PLease choose an option (1-4):\n" 
     choice = raw_input("1) input\n" \ 
          "2) change price and owner\n" \ 
          "3) list all cars\n" \ 
          "\"exit\" Exit") 

     if choice == "1": 
      input() 
     elif choice == "2": 
      print "Price" 
     elif choice == "3": 
      print "\n".join(map(str, listing)) 
     elif choice == "exit": 
      keep_running = False 

menu() 
相關問題