Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get 604 strings (URLs) and insert them into 4 lists
each list should take 151 string (URL)

What I have tried:

Python
first = []
second = []
third = []
forth = []

for i in range(1, 605):
    try:
        url = "http://url/" + str(i) + ".png"

        if i <= 151:
            first.append(url)

        elif i > 151:
            if i < 303:
                second.append(url)
            else:
                continue

        elif i > 302:
            if i < 453:
                third.append(url)
            else:
                continue
        else:
            forth.append(url)
    except Exception as e:
        print("Error accrued: " + str(e))

print("first:", first)
print("second:", second)
print("third:", third)
print("forth:", forth)


the results I get are only for the first and second list, the rest of the lists doesn't get the strings !!
list third and four results empty!!
any idea why this is happening
Posted
Updated 7-May-19 19:27pm

elif i > 151:
        if i < 303:
            second.append(url)
        else:
            continue


This says, if it's < 151, then check if it's < 303. If not, continue. THat is, do nothing.

Try
elif(i> 151 && i < 303)


I assume the and operator is standard in Python
 
Share this answer
 
Comments
Member 12173667 7-May-19 21:59pm    
I have tried it but same issue
Christian Graus 7-May-19 22:02pm    
Nonsense. If you wrote code that checked the range in one step, the next 'elif' will run as you'd hope. Whatever you thought you tried, you clearly need to paste it here so we can tell you what's wrong with it.
Member 12173667 7-May-19 22:07pm    
instead, I get urls from 152 to 604 filled in second list
for i in range(1, 605):
    try:
        url = "http://url/" + str(i) + ".png"

        if i <= 151:
            first.append(url)


        elif (i > 151 & i < 303):
            second.append(url)


        elif i > 302 & i < 453:
            third.append(url)
        else:
            forth.append(url)
    except Exception as e:
        print("Error accrued: " + str(e))
Christian Graus 7-May-19 22:09pm    
& is a bitwise and. Not what you want. A google tells me you want the word 'and'
Member 12173667 7-May-19 22:13pm    
Worked great!! thank you sir :)
Quote:
the results I get are only for the first and second list, the rest of the lists doesn't get the strings !!

Use the debugger to watch your code perform, it will help you to understand how it work, or don't work.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900