duff78
duff78

Reputation: 9

Chaning number in a variable name

I am still new, and the title isn't the most accurate. I apologize. I'm not going to go into the entire problem but I am having trouble with this specific part.

So heres the snippet of code:

num = 0
SongAttributes = myMusic.getSongAttributes(num)

while True:
    print(SongAttributes)
    num += 1

So 'myMusic.getSongAttributes()' is a list of 5000 songs. I want to be able to print the details of the first song, which would be 'myMusic.getSongAttributes(0)', then the second which would be 'myMusic.getSongAttributes(1)', and so fourth.

To do this, I set a variable 'num' equal to zero, and put it inside the (). When I put it in the loop, it should add 1 every time to loop occurs, thus changing the song details every loop. But instead it just repeats the same thing over again.

I am stuck here and my professor isn't responding, anything is appreciated.

Upvotes: 0

Views: 57

Answers (4)

Akshath Mahajan
Akshath Mahajan

Reputation: 248

Your assignment of the SongAttributes variable should be inside the while loop

num = 0 
while True: 
     SongAttributes = myMusic.getSongAttributes(num)
     print(SongAttributes) 
     num += 1

Also, you should add a line to terminate the loop as well, or use a for loop. Otherwise it'll infinitely keep going and whatever code you're trying to run will not process past that loop. Here's what it would look like

for num in range (len(myMusic.getSongAttributes())):
    print(myMusic.getSongAttributes(num))

Here I'm not assigning myMusic.getSongAttributes(num) to a variable to increase the performance of the code, you don't need to do that for small programs but it's a good habit to optimise.

I'm using len(myMusic.getSongAttributes()because you said it's a list of 5000 songs. If the length of the list is gonna be constant you might as well just think of that term as 5000.

Hope this helps

Upvotes: 0

Marsilinou Zaky
Marsilinou Zaky

Reputation: 1047

You set SongAttributes before the loop so it never gets updated, you should move it inside the loop as follows, (i would personally use for loop as it would take care of the counter and exit condition for me)

num = 0
while True:
    SongAttributes = myMusic.getSongAttributes(num)
    print(SongAttributes)
    num += 1

for i in range(len(myMusic)):
    Attributes = myMusic.getSongAttributes(num)
    print(SongAttributes)

Upvotes: 0

ItsPete
ItsPete

Reputation: 2368

You've already set SongAttributes to myMusic.getSongAttributes(0) & continually print that in your loop.

You need to call getSongAttributes in your loop:

num = 0

while True:

    SongAttributes = myMusic.getSongAttributes(num)

    print(SongAttributes)

    num += 1

Your loop will also loop infinitively if there's no way to exit it.

Upvotes: 1

Thomas Zwetyenga
Thomas Zwetyenga

Reputation: 86

You should put "SongAttributes = myMusic.getSongAttributes(num)" in the loop too.

You assigned the value to SongAttributes only before the loop so the value never changes

Upvotes: 2

Related Questions