2014-11-08 150 views
1

我試圖在用戶從字典值中的字典中選擇一個鍵時爲網站做最後的訪問日期和時間。替換字典中的值

這裏是代碼:

import webbrowser, time 

# -*- coding: utf-8 -*- 
web_pages = {1:('Google Scholar','webpage','N/A'), 
      2:('Moodle','webpage','N/A'), 
      3:('BBC','webpage','N/A'), 
      4:('Webmail','webpage','N/A')} 

##------last access date------------------------- 
def last_access(x, y): 
    ([x] +" "*10+ value[0] +" "*10 +value[1]+" "*10 +[y]+"\n") 

##-------output_structure_top--------------------------- 
def output_structure_top(): 
    print "--- MY BOOKMARK MANAGER --- "+"\n" 
    print "Number"+" "*10 +"Name"+" "*30+"Address"+" "*20+"Last accessed" 
    print "-"*90+"\n" 

##----output_structure_bottom------------------------ 
def output_structure_bottom(): 
    print "-"*60+"\n" 

while True: 
    ui = raw_input("""Enter the number to open a bookmark, 
    or enter + to add, - to delete, or x to exit: \n """) 
    litems = len(web_pages) 
    if ui >= litems: 
     ui = int(ui) 
     la = (time.strftime("%d/%m/%Y"))+" "+(time.strftime("%H:%M:%S")) 

     url = (web_pages[ui][1]) #get the url from the web_pages 

     webbrowser.open(url) #opens the url in a web browser 

     output_structure_top() #print out the top table structure 

     last_access(int(ui), la) #gets the date and time and replaces is it instead of N/A 

     output_structure_bottom() #print out the bottom table structure 

else: 
    print "invalid input!" 
    break 
` 

我所得到的是這樣的:

([x] +" "*10+ value[0] +" "*10 +value[1]+" "*10 +[y]+"\n") 
TypeError: can only concatenate list (not "str") to list 

什麼,我期待:

如果用戶選擇鍵2就會打開網址在瀏覽器中打印出來:

--- MY BOOKMARK MANAGER --- 

    Number__________Name_________________Address_________last accessed 

    1__________Google Scholar______________webpage_______N/A 

    2__________Moodle__________webpage__________08/11/2014 17:40:19 

    3__________BBC__________webpage__________N/A 

    4__________Webmail__________webpage__________N/A 

    ------------------------------------------------------------ 
+0

哪裏是 「值」 列表從哪裏來的,爲什麼你用的[X],而不是X? – 2014-11-08 18:22:53

+0

你也可以調用'str()'函數。 – Tico 2014-11-08 18:24:24

回答

0

如果xy的字符串,然後更換:

([x] +" "*10+ value[0] +" "*10 +value[1]+" "*10 +[y]+"\n") 

由:

(x +" "*10+ value[0] +" "*10 +value[1]+" "*10 +y+"\n")