#๐ Why doesnt the python code returns the track_number?
120 messages ยท Page 1 of 1 (latest)
@valid violet
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Sorry, but please share code, not screenshot
^ THIS
hm
How do I do that?
just copying code here?
Please share code like !co
!co
ยป message-content-intent
ยป message_content
ยป message_content_intent
ยป comparison
ยป codeblock
ยป contribute
...
wait a sec
!codeblock
def make_album(artist, album_title, track_number = ''):
album1 = {'artist': artist, 'album_title': album_title}
album2 = {'artist': artist, 'album_title': album_title}
album3 = {'artist': artist, 'album_title': album_title}
return album1
return album2
return album3
if track_number:
album1 = [track_number]
return album1
if track_number:
album2= [track_number]
return album1
if track_number:
album3 = [track_number]
return album1
album1 = make_album('suiiii', 'moresuii', '10')
print(album1)
Hey @valid violet!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
wha
d
album1 = {'artist': artist, 'album_title': album_title}
album2 = {'artist': artist, 'album_title': album_title}
album3 = {'artist': artist, 'album_title': album_title}
return album1
return album2
return album3
if track_number:
album1 = [track_number]
return album1
if track_number:
album2= [track_number]
return album1
if track_number:
album3 = [track_number]
return album1
album1 = make_album('suiiii', 'moresuii', '10')
print(album1)
This is wrong.
return album2
return album3
if track_number:
album1 = [track_number]
return album1
if track_number:
album2= [track_number]
return album1
if track_number:
album3 = [track_number]
return album1
It doesn't work.
What can I do to make it work or why doesnt it work?
You can only return once
Yes
:<
You can return multiple variables but all at the same time
what do you want using this code?
Want to build a function that have 3 dictionaries with the information artist and album_title and an optional information that is the track_number
and then like being able to give the value of the dictionaries and print them then
dictionaries are global in nature so you really don't have to return them
Do I need to return the track number then of the dictionary when the value of it is smth?
Why 3 dicts that have the same info?
So I can have 3 different albums containing different informations
Ive tried chaning the code to
def make_album(artist, album_title, track_number = ''):
album1 = {'artist': artist, 'album_title': album_title}
album2 = {'artist': artist, 'album_title': album_title}
album3 = {'artist': artist, 'album_title': album_title}
if track_number:
album1['track_number'] = track_number
return track_number
if track_number:
album2= [track_number]
return album3
if track_number:
album3 = [track_number]
return album2
album1 = make_album('suiiii', 'moresuii', '10')
print(album1)
Now it just prints 10
Hey @valid violet!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
every time you run this function you will get 3 dicts that are the same.
When I give the albums different values it prints the different values though
album1 = {'artist': artist, 'album_title': album_title}
album2 = {'artist': artist, 'album_title': album_title}
album3 = {'artist': artist, 'album_title': album_title}
if track_number:
album1['track_number'] = track_number
return track_number
if track_number:
album2['track_number'] = track_number
return album3
if track_number:
album3['track_number'] = track_number
return album2
album1 = make_album('suiiii', 'moresuii', '10')
print(album1)
album2 = make_album('suiiii', 'moresuii', '30')
print(album2)
album3 = make_album('suiiii', 'moresuii', '20')
print(album3)
Now it prints 10, 30, 20
but without the other information fo the dictionary
if you want to add a track you use this format dictionary['key'] = value
Hm can u show please how I can do that?
or you can make a list of dicts and append to add tracks
it returns the track_number which you've defined in the parameters
And how do I make it that it also returns the other values like artist and album_title?
A list of dicts do u mean like that?
albums = [album1, album2, album3]
It should store 3 dictionaries in a function with the values artist, album_title and an optional value which is track_number
and because track_number is optional I maked an if statement to only include it when it does store some value
or 3 of them
do you want to add track_number to all 3 albums?
ye wanna be able to add 3 different values of all 3 albums
omg it worked
finally
album1 = {'artist': artist, 'album_title': album_title}
album2 = {'artist': artist, 'album_title': album_title}
album3 = {'artist': artist, 'album_title': album_title}
if track_number:
album1['track_number'] = track_number
if track_number:
album2['track_number'] = track_number
return album2
if track_number:
album3['track_number'] = track_number
return album3
album1 = make_album('suiiii', 'moresuii', '10')
print(album1)
album2 = make_album('suiiii', 'moresuii', '20')
print(album2)
album3 = make_album('suiiii', 'moresuii', '30')
print(album3)
thats the code that works
Hey @valid violet!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
now it prints
{'artist': 'suiiii', 'album_title': 'moresuii', 'track_number': '10'}
{'artist': 'suiiii', 'album_title': 'moresuii', 'track_number': '20'}
{'artist': 'suiiii', 'album_title': 'moresuii', 'track_number': '30'}
I don't think so
and when I delete the value for track_number it only prints
{'artist': 'suiiii', 'album_title': 'moresuii'}
{'artist': 'suiiii', 'album_title': 'moresuii'}
{'artist': 'suiiii', 'album_title': 'moresuii'}
hm?
here
and also here
second one is without track_numbers
by just removing the value of track_numbers like it was supposed
now could you check your code like this? just add one line:
print(album1)
add this at the end
what?
don't you understand?
here another screenshot where I anded print(album1) again still works like its supposed to
Ive accidentally added the D to 10 after I executed it
add track_number to album2 and album3
whats not supposed to work?
again added D after executing it lol
๐ Today, I'd researched your code for a long time. Unfortunately your logic is wrong. now you are doing like this:
a=3
print(a)
b=4
print(b)
c=5
print(c)
why are you creating 3 albums in the function create_album? isn't it just supposed to create an album when you call it, instead of creating a whole load of albums?
completely logic error. you have to change it.
I wanted to have 3 different albums
He is right
then you call the function 3 times
1 * 3 = 3
evidently it does not
didn't you check my code?
i dont understand tbh
a=3
print(a)
b=4
print(b)
c=5
print(c)
you're sort of trying to do this:
def create_3_albums(...):
...
return created1, created2, created3
album1, album2, album3 = create_3_albums(...)```
but you're mixing it with this:
```py
def create_1_album(...):
...
return created
album1 = create_1_album(...)
album2 = create_1_album(...)
album3 = create_1_album(...)```
you have to pick one, you can't create a frankenstein's amalgamation of these 2 different approaches
What if you wanted an album 4?
using the create_1_album approach would be simpler, you just call the function 4 times then
Then Id just add an fourth?
But a function is supposed to be modular and you should not have to change it for more albums
precisely, the create_1_album approach is superior to the create_3_albums approach (in this case anyway)
yeah. different. every variable has their scope.
only 1 function is needed for this problem.
I have to go now, so see you later
def make_album(artist, album_title, track_number = ''):
album = {'artist': artist, 'album_title': album_title}
if track_number:
album['track_number'] = track_number
return album
album1 = make_album('suiiii', 'moresuii', '10')
print(album1)
album2 = make_album('suiiii', 'moresuii', '20')
print(album2)
album3 = make_album('suiiii', 'moresuii', '30')
print(album3)
album4 = make_album('suiiii', 'moresuii', '40')
print(album4)
This is all you need
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.