2016-12-18 39 views
0

我對Python完全陌生,我試圖編寫一個本質上充當鬧鐘的程序。我會提示用戶設置鬧鐘的具體時間,然後當該時間發生時,將播放txt文件中的YouTube視頻列表中的YouTube視頻。但是,我不太確定爲什麼我得到這個錯誤,因爲我仍然很大程度上不熟悉python語法。這裏是我的代碼:TypeError'str'對象不可調用 - Python

import time 

def addVideo(): 
    videoToAdd = raw_input("Enter the url of the video to add: ") 
    with open('alarmVideos.txt', 'w') as f: 
     f.write(videoToAdd + '\n') 

alarmTime = raw_input("When would you like to set your alarm to?: \nPlease use this format: 01:00\n") 

localTime = time.strftime("%H:%M") 

addVideo = raw_input("Would you like to add a video to your list? (y/n): \n") 

if addVideo == 'y' or addVideo == 'n': 
    addVideo() 


print "Your alarm is set to:", alarmTime 

我得到這個錯誤:

Traceback (most recent call last): 
    File "C:\Users\bkrause080\Desktop\Free Time Projects\LearningPythonProjects\alarmClock.py", line 15, in <module> 
    addVideo() 
TypeError: 'str' object is not callable 

如果它幫助用戶輸入Y/N他們是否沒有不希望添加視頻後,會出現此錯誤的名單。謝謝你的幫助!

+1

您有名稱衝突。爲字符串對象使用另一個名稱,所以'addVideo'只能引用函數 –

+0

您不能對函數名稱和其他變量使用'addVideo'。函數名稱也是變量。重命名一個或另一個。 –

+0

你在混淆Python。您正在使用** addVideo **作爲字符串變量的名稱以及函數的名稱。只需使用其中一個名稱。 –

回答

1

問題是因爲你被點名了兩個函數名和變量具有相同的名稱(addVideo),巨蟒「混淆的功能和variable.Rename它們中的任何一個:

import time 

def addVideo(): 
    videoToAdd = raw_input("Enter the url of the video to add: ") 
    with open('alarmVideos.txt', 'w') as f: 
     f.write(videoToAdd + '\n') 

alarmTime =raw_input("When would you like to set your alarm to?: \nPlease use this format: 01:00\n") 

localTime = time.strftime("%H:%M") 

add_Video = raw_input("Would you like to add a video to your list? (y/n): \n") 

if add_Video == 'y' or add_Video == 'n': 
    addVideo() 


print("Your alarm is set to:", alarmTime) 

輸出:

When would you like to set your alarm to?: 
Please use this format: 01:00 

Would you like to add a video to your list? (y/n): 
n 
Enter the url of the video to add: grg 
Your alarm is set to: 
+0

啊非常感謝你!我真的只有Java和C的經驗,所以Python的一些習慣! – bkrause404

0

你用一個字符串覆蓋功能addVideo():addVideo = raw_input("Would you like ...")

您需要重命名功能或varaible。

addVideoResult = raw_input("Would...") 

if addVideoResult == 'y' or addVideoResult == 'n': 
    addVideo() 
0

您需要raw_input("Would you like to add a video to your list? (y/n): \n")不同的名稱引用,因爲你是壓倒一切的功能:

run = raw_input("Would you like to add a video to your list? (y/n): \n") 
if run == 'y' or run == 'n': 
    addVideo()