2017-05-03 200 views
0

好吧,我已經完全更改我的代碼,以便客戶列表位於另一個列表中。現在我正在嘗試使用for循環引用每個客戶的單個列表。但是當我試圖訪問客戶列表中的單個值時,我得到一個TypeError:列表指示必須是整數或切片,而不是列表。這裏是代碼:Python:TypeError:列表索引必須是整數或切片,而不是列表

customers = [ ] 
customers.append(["Bilbo","Baggins","Dodge Dart", "120876","March 20 2017"]) 
customers.append(["Samwise"," Gamgee","Ford Tarus","190645","January 10 2017"]) 
customers.append(["Fredegar","Bolger","Nissan Altima", "80076","April 17 2017"]) 
customers.append(["Grima"," Wormtounge","Pontiac G6", "134657", "November 24 2016"]) 
customers.append(["Peregrin"," Took","Ford Focus", "143567", "February 7 2017"]) 
customers.append(["Meriadoc","Brandybuck","Ford Focus", "143567", "February 19 2017"]) 



print("At Holden's Oil Change we use our custom built Python program to keep \ 
track of customer records \ 
and to display our company logo!!") 
time.sleep(7) 

print("Select and option from the menu!") 

QUIT = '4' 

COMMANDS = ('1','2','3','4') 

MENU = """1 Current Customers 
2 New Customers 
3 Company Logo 
4 Quit""" 

def main(): 
    while True: 
     print(MENU) 
     command = realCommand() 
     commandChoice(command) 
     if command == QUIT: 
      print("Program Ended") 
      break 

def realCommand(): 
    while True: 
     command = input("Select an option: ") 
     if not command in COMMANDS: 
      print("That is not an option!") 
     else: 
      return command 

def commandChoice(command): 
    if command == '1': 
     oldCust() 
    elif command == '2': 
     newCust() 
    elif command == '3': 
     Turt() 

def oldCust(): 
    print("%6s%12s%20s%24s%22s" % ("First Name", "Last Name", "Car Make & Model", "Mileage Last Service", "Date Last Oil Change")) 
    for i in customers: 
     print("%8s%18s%22s%24s%32s" % (customers[i][0],customers[i][1],customers[i][2],customers[i][3],customers[i][4])) 

函數oldCust()是當for循環運行時出現錯誤的地方,它給出了類型錯誤。我嘗試了幾種不同的方式,但每種方式都會發回相同的錯誤。 這裏是被返回的整個錯誤:

Traceback (most recent call last): 
    File "C:\Users\hdaug\Documents\Holden's Personal\Spring 2016-2017\CSCI 1121\HoldensOilChange.py", line 264, in <module> 
    main() 
    File "C:\Users\hdaug\Documents\Holden's Personal\Spring 2016-2017\CSCI 1121\HoldensOilChange.py", line 49, in main 
    commandChoice(command) 
    File "C:\Users\hdaug\Documents\Holden's Personal\Spring 2016-2017\CSCI 1121\HoldensOilChange.py", line 66, in commandChoice 
    oldCust() 
    File "C:\Users\hdaug\Documents\Holden's Personal\Spring 2016-2017\CSCI 1121\HoldensOilChange.py", line 79, in oldCust 
    print("%8s%18s%22s%24s%32s" % (customers[i][0],customers[i][1],customers[i][2],customers[i][3],customers[i][4])) 
TypeError: list indices must be integers or slices, not list 
+2

您是否可以將代碼縮小到實際需要查看的位置? –

+1

我不認爲你真的想把每個客戶放在自己的變量中;你會發現把它們放在更好的列表中。 –

+0

好吧,我擺脫了所有的newCust()功能代碼 – hdaugh90

回答

0

要保存輸入到多個增值經銷商,做這樣的事情:

tmp = raw_input(">>> ") 
var1 = tmp 
var2 = tmp 
var3 = tmp 

要使用輸入多次,使用函數:

def function(num): 
    i = 0 
    while i < num: 
    tmp = raw_input(">>> ") 

function(3) 
var1 = tmp 
var2 = tmp 
var3 = tmp 
1

首先使用list來存儲客戶變量。然後,您可以輕鬆地在列表中添加新客戶。

這是你的問題的完整的解決方案:

""" 
Program that is used to store service 
records for prior customers or prompt 
user for customer information for new customers. 

The program also uses Turtle Graphics to display 
the company logo. 
""" 
#Import time module for delays in program 
import time 

#Define current customers 

customer_list = [] 
customer_list.append(["Bilbo","Baggins","Dodge Dart", "120876","March 20 2017"]) 
customer_list.append(["Samwise"," Gamgee","Ford Tarus","190645","January 10 2017"]) 
customer_list.append(["Fredegar","Bolger","Nissan Altima", "80076","April 17 2017"]) 
customer_list.append(["Grima"," Wormtounge","Pontiac G6", "134657", "November 24 2016"]) 
customer_list.append(["Peregrin"," Took","Ford Focus", "143567", "February 7 2017"]) 
customer_list.append(["Meriadoc","Brandybuck","Ford Focus", "143567", "February 19 2017"]) 


#Announce the company and what our program does 
print("At Holden's Oil Change we use our custom built Python program to keep \ 
track of customer records \ 
and to display our company logo!!") 
time.sleep(7) 

#Tell the user what to do 
print("Select and option from the menu!") 

#Make the menu and menu options for the user 
QUIT = '4' 

COMMANDS = ('1','2','3','4') 

MENU = """1 Current Customers 
2 New Customers 
3 Company Logo 
4 Quit""" 

#Define how the menu works if quit option selected 
def main(): 
    while True: 
     print(MENU) 
     command = realCommand() 
     commandChoice(command) 
     if command == QUIT: 
      print("Program Ended") 
      break 

#Define what happens if a invalid command is entered or a correct command is entered 
def realCommand(): 
    while True: 
     command = input("Select an option: ") 
     if not command in COMMANDS: 
      print("That is not an option!") 
     else: 
      return command 

#Command selection and running 
def commandChoice(command): 
    if command == '1': 
     oldCust() 
    elif command == '2': 
     newCust() 
    elif command == '3': 
     Turt() 

#Runs old customer selection 
def oldCust(): 
    #Print list of customers for the user to select from. 
    print("%6s%12s%20s%24s%22s" % ("First Name", "Last Name", "Car Make & Model", "Mileage Last Service", "Date Last Oil Change")) 
    for customer in customer_list: 
     for value in customer: 
      print(value,end="\t") 
     print("") 

#Request response from user and define what happens depending on the input. 
    response = input("Ener a customers last name from the list: ") 
    customer_search_result = 0 
    for customer in customer_list: 
     if response.lower() == customer[1].lower(): 
      user_milage = input("Enter current vehicle mileage: ") 
      user_date = input("Enter todays date (Month Day Year Format): ") 
      print("%6s%12s%20s%24s%22s" % ("First Name", "Last Name", "Car Make & Model", "Mileage Last Service", "Date Last Oil Change")) 
      print("%9s%13s%19s%25.9s%34s" % (customer[0], customer[1], customer[2], customer[3], customer[4])) 
      print("Have a great day!") 
      customer_search_result=1 

    if customer_search_result==0: 
     print("That is not a current customer!") 
     time.sleep(2) 
     #Request user input wheter they want to input new customer info or try another customer name. 
     nonCustResponse = input("Choose 1 to re-enter customer name or 2 to enter new customer info: ") 
     #if statement that decides what to do with the user input 
     if nonCustResponse == "1": 
      oldCust() 
     elif nonCustResponse == '2': 
      #Send the user to the newCust function if they enter a non-current customer 
      newCust() 
      #If the customer enters an invalid option the program restarts 
     else: 
      print("That is not an option. Program restarting") 
      time.sleep(3) 


#Prompts user for information for the new customer 
def newCust(): 
    #Make an empty list for the new customer to be assigned to 
    new_customer = [" "," "," "," "," "] 
    #Request user input for the new customer information 
    new_customer[0] = input("Enter the customers firsts name: ") 
    new_customer[1] = input("Enter the customers last name: ") 
    new_customer[2] = input("Enter the customers vehilce (Make Model): ") 
    new_customer[3] = input("Enter the vehicle mileage: ") 
    new_customer[4] = input("Enter today's date (Month Day Year): ") 
    print("%6s%12s%20s%24s%22s" % ("First Name", "Last Name", "Car Make & Model", "Mileage Last Service", "Date Last Oil Change")) 
    print("%8s%13s%22s%25s%34s" % (new_customer[0], new_customer[1], new_customer[2], new_customer[3], new_customer[4])) 
    customer_list.append(new_customer) 
if __name__=='__main__': 
    main() 

我已經更新了你的原代碼的一些部分,使其運行能。此代碼可以即興創建。

+0

當我將所有這些內容都寫入代碼時,它不會將我的菜單項鍊接到命令選擇用戶輸入 – hdaugh90

+0

我已經調用了'''__name__'''中的主要方法。所以,你可以照顧這個。 – arsho

1

你就可以允許一些轉義碼,當你完成了你的輸入,即只是空字符串:

customers = [] 
new_customer = input('> ') 
while new_customer: 
    customers.append(new_customer) 
    new_customer = input('> ') 

因此,當用戶已經達到不寫任何東西進入,輸入完成。如果您想將「退出代碼」更改爲更復雜的內容,請使用以下代碼:

customers = [] 
exit_code = 'stop' 
new_customer = input('> ') 
while new_customer != exit_code: 
    customers.append(new_customer) 
    new_customer = input('> ') 
相關問題