Question
Drag and drop. Options:
# Loop to add as many songs as user wishes
# Global Variable
# Makes global variable accessible
# Song is marked as favorite
# Function to mark song as "favorite"
Code:
playlist = []
songs_created = 0 (insert option)
class Song:
def _init_(self,title,artist):
self.title = title
self.artist = artist
self.favorite = False
def add_to_favorites(self):
self.favorite = True (insert option)
def display_info(self):
print(f"{self.title}, by {self.artist}. ")
# Song creation function
def create_song():
global songs_created (insert option)
title = input("Name the title of a song: ")
artist = input("Name who sings this song: ")
New_song = Song(title, artist)
favorite = input("Is this song one of your favorites? (Y or N)")
if favorite.lower() == "y":
new_song.add_to_favorites() (insert option)
# Loop to add as many songs as user wishes
# Global Variable
# Makes global variable accessible
# Song is marked as favorite
# Function to mark song as "favorite"
Code:
playlist = []
songs_created = 0 (insert option)
class Song:
def _init_(self,title,artist):
self.title = title
self.artist = artist
self.favorite = False
def add_to_favorites(self):
self.favorite = True (insert option)
def display_info(self):
print(f"{self.title}, by {self.artist}. ")
# Song creation function
def create_song():
global songs_created (insert option)
title = input("Name the title of a song: ")
artist = input("Name who sings this song: ")
New_song = Song(title, artist)
favorite = input("Is this song one of your favorites? (Y or N)")
if favorite.lower() == "y":
new_song.add_to_favorites() (insert option)
Answers
Answered by
GPT-4o mini
Here's how you can correctly organize the code with the provided options for each insertion point. The goal is to create a song class, allow users to create songs, and mark them as favorites.
Here is the complete code with the appropriate options inserted:
```python
# Global Variable
songs_created = 0
class Song:
def __init__(self, title, artist): # Correct constructor initialization
self.title = title
self.artist = artist
self.favorite = False
def add_to_favorites(self): # Function to mark song as "favorite"
self.favorite = True
def display_info(self):
print(f"{self.title}, by {self.artist}. Favorite: {self.favorite}")
# Loop to add as many songs as user wishes
def create_song():
global songs_created # Makes global variable accessible
title = input("Name the title of a song: ")
artist = input("Name who sings this song: ")
new_song = Song(title, artist) # Correctly named variable
favorite = input("Is this song one of your favorites? (Y or N): ")
if favorite.lower() == "y":
new_song.add_to_favorites() # Calls function to mark song as "favorite"
playlist.append(new_song) # Add song to the playlist
songs_created += 1 # Increment number of songs created
# Main loop to create songs
while True:
create_song()
another = input("Do you want to add another song? (Y or N): ")
if another.lower() != 'y':
break
# Display all songs in the playlist
print("\nYour Playlist:")
for song in playlist:
song.display_info()
```
### Explanation of insertions:
1. **Global Variable**: `songs_created = 0` indicates that there are initially no songs created.
2. **Makes global variable accessible**: `global songs_created` allows the function `create_song` to modify the `songs_created` variable.
3. **Function to mark song as "favorite"**: `def add_to_favorites(self)` allows a song to be marked as a favorite.
4. **Correctly named variable**: Use `new_song` for the created song instance.
The code will effectively run a loop where the user can create multiple songs, mark them as favorites, and display the playlist at the end.
Here is the complete code with the appropriate options inserted:
```python
# Global Variable
songs_created = 0
class Song:
def __init__(self, title, artist): # Correct constructor initialization
self.title = title
self.artist = artist
self.favorite = False
def add_to_favorites(self): # Function to mark song as "favorite"
self.favorite = True
def display_info(self):
print(f"{self.title}, by {self.artist}. Favorite: {self.favorite}")
# Loop to add as many songs as user wishes
def create_song():
global songs_created # Makes global variable accessible
title = input("Name the title of a song: ")
artist = input("Name who sings this song: ")
new_song = Song(title, artist) # Correctly named variable
favorite = input("Is this song one of your favorites? (Y or N): ")
if favorite.lower() == "y":
new_song.add_to_favorites() # Calls function to mark song as "favorite"
playlist.append(new_song) # Add song to the playlist
songs_created += 1 # Increment number of songs created
# Main loop to create songs
while True:
create_song()
another = input("Do you want to add another song? (Y or N): ")
if another.lower() != 'y':
break
# Display all songs in the playlist
print("\nYour Playlist:")
for song in playlist:
song.display_info()
```
### Explanation of insertions:
1. **Global Variable**: `songs_created = 0` indicates that there are initially no songs created.
2. **Makes global variable accessible**: `global songs_created` allows the function `create_song` to modify the `songs_created` variable.
3. **Function to mark song as "favorite"**: `def add_to_favorites(self)` allows a song to be marked as a favorite.
4. **Correctly named variable**: Use `new_song` for the created song instance.
The code will effectively run a loop where the user can create multiple songs, mark them as favorites, and display the playlist at the end.
There are no human answers yet. A form for humans to post answers is coming very soon!