2016-07-30 12 views
-2

So schrieb ich heute ein relativ kindisches Programm in Python für das Öffnen eines Freizeitvideos auf YouTube nach einer bestimmten Zeit. Mehr wie eine Pausenzeit Sache, so dass es eine zufällige Auswahl aus einer Liste von URLs geöffnet. Hier ist der Code:Compiler gibt ungültigen Syntaxfehler bei Verwendung von time.ctime() zurück

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

Ihr Druck verfehlt ein Komma und die 'while' Block ist nicht eingerückt. –

+0

Python-Compiler, lolwut. –

+0

@PeterNimroot Was ist damit? –

Antwort

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

Was ist anders? Könnten Sie bitte etwas Kontext hinzufügen? – ppperry

+0

Während dieses Code-Snippet die Frage lösen kann, hilft [einschließlich einer Erklärung] (// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) wirklich, die Qualität Ihres Posts zu verbessern. Denken Sie daran, dass Sie die Frage für Leser in der Zukunft beantworten, und diese Leute könnten die Gründe für Ihren Codevorschlag nicht kennen. Bitte versuchen Sie auch nicht, Ihren Code mit erläuternden Kommentaren zu überladen, da dies die Lesbarkeit sowohl des Codes als auch der Erklärungen verringert! – FrankerZ

+0

@FrankerZ FFFFixed! – pmus