2017-05-30 109 views
0

讓我們看看下面的例子:如何在python中一個接一個地打開多個網頁?

import webbrowser 
Cn=('Acharya Girish Chandra Bose College','AJC Bose College','Ananda Mohan College','Asutosh College','Bangabasi College','Barrackpore Rastraguru Surendranath College','Basanti Devi College') 

Cnw={'Acharya Girish Chandra Bose College':'http://www.agcbosecollege.org/','AJC Bose College':'http://www.ajcbosecollege.org/','Ananda Mohan College':'http://anandamohancollege.ac.in/','Asutosh College':'http://www.asutoshcollege.in/','Bangabasi College':'http://bangabasi.org/home.php','Barrackpore Rastraguru Surendranath College':'http://www.brsnc.org/','Basanti Devi College':'http://www.basantidevicollege.edu.in/'} 

yy=['Y','Yes','YES','y'] 
nn=['N','NO','no','No','n'] 
i=0 
while i<=range(len(Cn)): 
    print 'College Name : ',Cn[i] 
    a=raw_input('Do u want to visit the website ? (Y/N) :') 
    if a in yy: 
     webbrowser.open(Cnw[Cn[i]]) 
    elif a in nn: 
     break 
    i=i+1 

這個程序能正常工作,但我想打開一個網頁後,打開下一個網站。該程序只打開一個網站,然後打開另一個網站,但每次跳過一個網站。在這裏:
Image link here

從列表中打開一個網站後,它會增加我的I = I + 1,再次問我打開下一個網站。請建議做這個

回答

0

我不能確定,爲什麼你使用range(),只需使用len(Cn)就足夠了,但是你應該把它改爲i<len(Cn)讓你不用去了指數上的最後一次迭代while循環。

此外,當您在elif語句中輸出break時,會結束循環。我不確定這是否是您想要做的事情,如果您寧願繼續解析列表,只需刪除elif聲明即可。您也不需要您的nn沒有變化的列表。

import webbrowser 
Cn=('Acharya Girish Chandra Bose College','AJC Bose College','Ananda Mohan College','Asutosh College','Bangabasi College','Barrackpore Rastraguru Surendranath College','Basanti Devi College') 

Cnw={'Acharya Girish Chandra Bose College':'http://www.agcbosecollege.org/','AJC Bose College':'http://www.ajcbosecollege.org/','Ananda Mohan College':'http://anandamohancollege.ac.in/','Asutosh College':'http://www.asutoshcollege.in/','Bangabasi College':'http://bangabasi.org/home.php','Barrackpore Rastraguru Surendranath College':'http://www.brsnc.org/','Basanti Devi College':'http://www.basantidevicollege.edu.in/'} 

yy=['Y','Yes','YES','y'] 
nn=['N','NO','no','No','n'] 
i=0 
while i<len(Cn): 
    print 'College Name : ',Cn[i] 
    a=raw_input('Do u want to visit the website ? (Y/N) :') 
    if a in yy: 
     webbrowser.open(Cnw[Cn[i]]) 
    #elif a in nn: 
    # break 
    i=i+1 

這給了我下面的輸出:

College Name : Acharya Girish Chandra Bose College 
Do u want to visit the website ? (Y/N) :n 
College Name : AJC Bose College 
Do u want to visit the website ? (Y/N) :n 
College Name : Ananda Mohan College 
Do u want to visit the website ? (Y/N) :n 
College Name : Asutosh College 
Do u want to visit the website ? (Y/N) :n 
College Name : Bangabasi College 
Do u want to visit the website ? (Y/N) :n 
College Name : Barrackpore Rastraguru Surendranath College 
Do u want to visit the website ? (Y/N) :n 
College Name : Basanti Devi College 
Do u want to visit the website ? (Y/N) :n 
>>> ================================ RESTART ================================ 
>>> 
College Name : Acharya Girish Chandra Bose College 
Do u want to visit the website ? (Y/N) :y 
College Name : AJC Bose College 
Do u want to visit the website ? (Y/N) :y 
College Name : Ananda Mohan College 
Do u want to visit the website ? (Y/N) :y 
College Name : Asutosh College 
Do u want to visit the website ? (Y/N) :y 
College Name : Bangabasi College 
Do u want to visit the website ? (Y/N) :y 
College Name : Barrackpore Rastraguru Surendranath College 
Do u want to visit the website ? (Y/N) :y 
College Name : Basanti Devi College 
Do u want to visit the website ? (Y/N) :y 
>>> 
+1

非常感謝您的回答@ xdflames – MKS

相關問題