好吧,我已經完全更改我的代碼,以便客戶列表位於另一個列表中。現在我正在嘗試使用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
您是否可以將代碼縮小到實際需要查看的位置? –
我不認爲你真的想把每個客戶放在自己的變量中;你會發現把它們放在更好的列表中。 –
好吧,我擺脫了所有的newCust()功能代碼 – hdaugh90