Reputation: 19
I'm completely new to Python and i'm trying to write a program that essentially works as an alarm clock. I prompt the user for a specific time to set the alarm to and then when that time occurs, a youtube video taken from a list of youtube videos in a txt file will play. However, I'm not quite sure why i'm getting this error, as i'm still largely unfamiliar with python syntax. Here's my code:
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
I'm getting this error:
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
If it helps this error occurs after the user enters y/n to whether not not they want to add a video to their list. Thanks for any help!
Upvotes: 0
Views: 767
Reputation: 5704
Problem is since you named both the function name and variable with the same name(addVideo),Python 'confuses' the function and variable.Rename any one of them:
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)
Output:
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:
Upvotes: 1
Reputation: 535
You need a different name reference for raw_input("Would you like to add a video to your list? (y/n): \n")
since you are overriding the function:
run = raw_input("Would you like to add a video to your list? (y/n): \n")
if run == 'y' or run == 'n':
addVideo()
Upvotes: 0
Reputation: 18106
You are overwriting the function addVideo() with a string: addVideo = raw_input("Would you like ...")
You need to rename the function or the varaible.
addVideoResult = raw_input("Would...")
if addVideoResult == 'y' or addVideoResult == 'n':
addVideo()
Upvotes: 0