#๐Ÿ”’ Why doesnt the python code returns the track_number?

120 messages ยท Page 1 of 1 (latest)

neon skyBOT
#

@valid violet

Python help channel opened

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.

valid violet
#

first picture was cut a bit here full

#

oh wait

storm junco
#

Sorry, but please share code, not screenshot

floral pumice
valid violet
#

hm

valid violet
#

just copying code here?

storm junco
#

Please share code like !co

valid violet
#

!co

neon skyBOT
#
Did you mean...

ยป message-content-intent
ยป message_content
ยป message_content_intent
ยป comparison
ยป codeblock
ยป contribute

valid violet
#

...

storm junco
#

wait a sec

valid violet
#

!codeblock

neon skyBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

valid violet
#
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)
neon skyBOT
#

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.
valid violet
#

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)
storm junco
#

It doesn't work.

valid violet
#

yee

#

thats the problem

valid violet
green gazelle
#

You can only return once

valid violet
#

hm

#

hmm

green gazelle
#

Yes

valid violet
#

:<

green gazelle
#

You can return multiple variables but all at the same time

storm junco
#

what do you want using this code?

valid violet
#

and then like being able to give the value of the dictionaries and print them then

green gazelle
#

dictionaries are global in nature so you really don't have to return them

valid violet
green gazelle
#

Why 3 dicts that have the same info?

valid violet
#

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

neon skyBOT
#

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.
green gazelle
#

every time you run this function you will get 3 dicts that are the same.

valid violet
# green gazelle 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

green gazelle
#

if you want to add a track you use this format dictionary['key'] = value

valid violet
green gazelle
#

or you can make a list of dicts and append to add tracks

green leaf
valid violet
valid violet
#

albums = [album1, album2, album3]

green leaf
#

what's the purpose of your program

valid violet
#

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

green leaf
#

do you want to add track_number to all 3 albums?

valid violet
#

omg it worked

#

finally

green leaf
#

ok. I will show you the code

#

just a few minutes later

valid violet
#
    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

neon skyBOT
#

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.
valid violet
#

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'}

green leaf
#

I don't think so

valid violet
#

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'}

valid violet
green leaf
#

does it work really?

#

can you run it again?

#

and show me the result

valid violet
#

here

#

and also here

#

second one is without track_numbers

#

by just removing the value of track_numbers like it was supposed

green leaf
#

now could you check your code like this? just add one line:

print(album1)
#

add this at the end

green leaf
#

don't you understand?

valid violet
#

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

green leaf
#

add track_number to album2 and album3

valid violet
#

again added D after executing it lol

green leaf
#

๐Ÿ˜‡ 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)
mighty inlet
#

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?

green leaf
#

completely logic error. you have to change it.

valid violet
mighty inlet
#

1 * 3 = 3

valid violet
#

mine also works

mighty inlet
green leaf
#

didn't you check my code?

green leaf
#
a=3
print(a)
b=4
print(b)
c=5
print(c)
valid violet
#

ye

#

eh?

green leaf
#

you just doing like that

#

you have to know this. I will send you image

mighty inlet
#

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
green gazelle
#

What if you wanted an album 4?

mighty inlet
#

using the create_1_album approach would be simpler, you just call the function 4 times then

green leaf
valid violet
valid violet
#

theyre different

green gazelle
#

But a function is supposed to be modular and you should not have to change it for more albums

mighty inlet
#

precisely, the create_1_album approach is superior to the create_3_albums approach (in this case anyway)

green leaf
#

only 1 function is needed for this problem.

#

I have to go now, so see you later

green gazelle
#
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

valid violet
#

yoo thanks

#

!close

neon skyBOT
#
Python help channel closed

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.