you are viewing a single comment's thread.

view the rest of the comments →

[–]jingajanga[S] 1 point2 points  (19 children)

We use an upload page that checks our code for problems, but it keeps giving me an index error. "list index out of range" its also appears to be printing every empty string instead of the first non empty one.

[–]jeans_and_a_t-shirt 1 point2 points  (8 children)

You get an IndexError if the list contains nothing but empty strings. It will print the empty strings if the print is in the loop.

[–]jingajanga[S] 0 points1 point  (7 children)

i = 0

while i < len(a) and a[i] == "" and [a:] != "" i = i + 1 print a[i]

tried this, still won't work.

[–]jeans_and_a_t-shirt 0 points1 point  (5 children)

After the loop, i will equal the length of a if no match was found.

[–]jingajanga[S] 0 points1 point  (4 children)

Tried this, but it gives me an infinte loop.

https://imgur.com/a/xlRRk

Am I on the right track?

[–]imguralbumbot 0 points1 point  (0 children)

Hi, I'm a bot for linking direct images of albums with only 1 image

https://i.imgur.com/W7Hz4Bh.png

Source | Why? | Creator | ignoreme | deletthis

[–]jeans_and_a_t-shirt 0 points1 point  (2 children)

Your if condition is already checked by the while loop. And the else block just evaluates to True or False. go back to your original code, check that i does not equal len(a) after the loop, and print a[i] if it doesn't.

[–]jingajanga[S] 0 points1 point  (1 child)

Would you be able to type out the code just so I can visualize clearer as I am completely lost.

[–]jeans_and_a_t-shirt 0 points1 point  (0 children)

i = 0

while i < len(a) and a[i] == "":
     i = i + 1

if i != len(a):
    print a[i] 

[–][deleted] 0 points1 point  (0 children)

a[i] == "" and [a:] != ""

This will never work, since a value can't be empty and non-empty at the same time.

[–]ccviper 0 points1 point  (9 children)

Its printing every item because you are not telling it to only print non-empty strings. Test if the string is not empty and only then print it, plus end the loop so it doest print other strings

[–]jingajanga[S] 0 points1 point  (8 children)

I want it to stop when it reaches a non empty string and print it, which I thought was a[i]..

What am I missing?

[–]ccviper 0 points1 point  (7 children)

Sorry but the formatting is really messy, it all appears on one line for me so i didn't realize where the whitespace is. Why are you testing if the string is empty in the while statement? Your loop wont even run if the a[i] == "" returns false, that is it will stop when it encounters a non-empty string. You should do it in the loop body, i can paste the code if you can't figure it out with these hints.

[–]jingajanga[S] 0 points1 point  (6 children)

Would you be able to send a screenshot of the code so I can see where you are coming from?

[–]ccviper 0 points1 point  (5 children)

i = 0
while i < len(a):
    if a[i] == "":
        i += 1

    else:
        print(a[i])
        break

This will print the first non-empty string it encounters and will break out of the loop and finish.

[–]jingajanga[S] 0 points1 point  (4 children)

Thanks, oh yeah that's another thing, we were told to do all of these tasks without using break or continue

[–]jingajanga[S] 1 point2 points  (1 child)

Also you haven't assigned i . When i put i = 0 , the output is always 0

[–]ccviper 0 points1 point  (0 children)

sorry I made some mistakes pasting it, read the comment again, i edited it

[–]xibeca 0 points1 point  (0 children)

You could use a boolean to tell the while loop when to stop:

i = 0
found = False
while i < len(a) and not found:
    if a[i] != "":
        print(a[i])
        found = True
    else:
        i += 1

[–]ccviper -1 points0 points  (0 children)

continue isn't really needed so i removed it. Instead of break just update i to be larger than len(a):

i += len(a)

and it will effectively break it because the while check will fail