2016-07-30 73 views
-2

因此,今天我寫了一個相對幼稚的Python程序,用於在一段時間後在YouTube上打開娛樂視頻。更像是休息時間的事情,以便它從URL列表中隨機挑選一個。下面的代碼:編譯器在使用time.ctime()時返回無效語法錯誤

import os 
import sys 
import webbrowser 
from time import * 
import random 

print("Hello! This program was started at " time.ctime()) 

totalBreaks = 5000 
breaksTaken = 0 

url = ['http://www.youtube.com/watch?v=OXWrjWDQh7Q', 'https://www.youtube.com/watch? v=yNLdblFQqsw', 'https://www.youtube.com/watch?v=tD4HCZe-tew',   'https://www.youtube.com/watch?v=GTyN-DB_v5M', 'https://www.youtube.com/watch?v=n49qi-dU9IE', 'https://www.youtube.com/watch?v=2iFa5We6zqw', 'https://www.youtube.com/watch?v=KEI4qSrkPAs', 'https://www.youtube.com/watch?v=yzTuBuRdAyA', 'https://www.youtube.com/watch?v=_kqQDCxRCzM', 'https://www.youtube.com/watch?v=u2cphuMbqfc'] 

while (breaksTaken > totalBreaks) : 
time.sleep(60) 
webbroswer.open(choice.random(url)) 
+0

您的打印錯過了一個逗號,你的'while'塊不是縮進。 –

+0

Python編譯器,lolwut。 –

+0

@PeterNimroot呢? –

回答

1
import os 
import sys 
import webbrowser 
import time 

# Added explanation #0: 
# Always try to avoid import * 
# and if still you do 'from time import sleep' or 'from time import *' 
# then there will be different namespace, so you'd use: sleep(1) 
# but not time.sleep(1) -- and this is not Pythonic way. 

import random 

print("Hello! This program was started at %s " % time.ctime()) 

# Added explanation #1: 
# You should concatenate output in print ^^^^^^^^^^ statement 

totalBreaks = 5000 
breaksTaken = 0 

url = ['http://www.youtube.com/watch?v=OXWrjWDQh7Q', 
     'https://www.youtube.com/watch?v=yNLdblFQqsw', 
     'https://www.youtube.com/watch?v=tD4HCZe-tew'] 

while (breaksTaken > totalBreaks): 
    time.sleep(60) 
    webbroswer.open(random.choice(url)) 
# Added explanation #2: 
# You should use random.choice(), not a choice.random() 

# That's all, folks! 
+0

有什麼不同?你能否添加一些上下文? – ppperry

+0

雖然這段代碼可能會解決這個問題,但[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要使用解釋性註釋來擠佔代碼,因爲這會降低代碼和解釋的可讀性! – FrankerZ

+0

@FrankerZ FFFFixed! – pmus

相關問題