2013-08-06 117 views
-1

有什麼錯我的代碼給我的錯誤: 類型錯誤:指數的名單必須是整數,而不是str的類型錯誤:索引列表必須是整數,而不是str的

這裏是我的代碼:

print("This programe will keep track of your TV schedule.") 
Finish = False 
Show = [] 
ShowStart = [] 
ShowEnd = [] 
while not Finish: 
print() 
ShowName = input("What is the shows name?: ") 
if ShowName == "": 
    Finish = True 
else: 
    ShowStartTime = input("What time does the show start?: ") 
    ShowEndTime = input("What time does the show end?: ") 
    Show.append(ShowName) 
    ShowStart.append(ShowStartTime) 
    ShowEnd.append(ShowEndTime) 
print("{0:<10} | {1:<10} | {2:<10} ".format("Show Name", "Start Time", "End Time")) 
for each in Show: 
print("{0:<10} | {1:<10} | {2:<10} ".format(Show[each], ShowStart[each], ShowEnd[each])) 
input() 
+0

當你用Google搜索「類型錯誤:索引列表必須是整數,不是str「 - 對你沒有幫助/你從結果中不瞭解什麼? –

回答

0

您上一次循環錯誤。試試這個:

for each in range(len(Show)): 
    print("{0:<10} | {1:<10} | {2:<10} ".format(Show[each], ShowStart[each], ShowEnd[each])) 

(你的3名名單應在字典中的一個列表的方式合併:

print("This programe will keep track of your TV schedule.") 
Finish = False 
shows = [] 
while not Finish: 
    ShowName = input("What is the shows name?: ") 
    if ShowName == "": 
     Finish = True 
    else: 
     ShowStartTime = input("What time does the show start?: ") 
     ShowEndTime = input("What time does the show end?: ") 
     shows.append({'name': ShowName, 'start': ShowStartTime, 'end': ShowEndTime}) 

print("{0:<10} | {1:<10} | {2:<10} ".format("Show Name", "Start Time", "End Time")) 

for item in shows: 
    print("{0:<10} | {1:<10} | {2:<10} ".format(item['name'], item['start'], item['end'])) 
    # Or the more pythonic way: 
    print("{name:<10} | {start:<10} | {end:<10} ".format(**item) 
input() 

+0

如果你打算使用'dict',那麼使用命名參數比如'print(「{name:<10} | {start:<10} | {end:<10}」會更好。 .format(** item)'而不是 –

+0

當然,但我不想過於複雜化代碼(我不認爲作者知道'** var'語法:-))。我添加您的解決方案作爲替代。 –

+0

謝謝你真的爲我清除了這一點,我也學到了新的東西感謝。我是一個你肯定猜到的小白菜:D –

相關問題