我是Python的新手,我正在尝试编写一个基本上用作闹钟的程序。我提示用户在特定时间设置闹钟,然后当该时间发生时,将播放从txt文件中的YouTube视频列表中获取的YouTube视频。但是,我不太清楚为什么我会收到这个错误,因为我仍然对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后是否有助于发生此错误,他们是否想要将视频添加到其列表中。谢谢你的帮助!
答案 0 :(得分:1)
问题是因为你将函数名和变量命名为同名(addVideo),Python&#39; confuse&#39;函数和变量。重命名其中任何一个:
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:
答案 1 :(得分:0)
您正在使用字符串addVideo = raw_input("Would you like ...")
您需要重命名该功能或变通。
addVideoResult = raw_input("Would...")
if addVideoResult == 'y' or addVideoResult == 'n':
addVideo()
答案 2 :(得分: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()