Aqsa
Aqsa

Reputation: 67

basic python for loop

I am new to python could someone tell me what is the problem with this code:

#!/usr/bin/python
nl=[] 
for x in range(1500, 2700):
     i=0
     if (x%7==0) and (x%5==0):
         nl[i]=x
     i=i+1
print (','.join(nl))

Upvotes: 0

Views: 1074

Answers (8)

Julien Spronck
Julien Spronck

Reputation: 15433

Also, in Python, you can use a so-called list comprehension to get the same result in one line:

nl = [x for x in range(1500, 2700) if (x%7==0) and (x%5==0)]

This provides a list of int. To print it, you need to convert the int to string:

print(','.join(map(str, nl)))

Or you can directly create a list of strings:

nl = [str(x) for x in range(1500, 2700) if (x%7==0) and (x%5==0)]
print(','.join(nl))

If all you want is printing it, everything can be done in one single line:

print(','.join(str(x) for x in range(1500, 2700) if (x%7==0) and (x%5==0)))    

Upvotes: 0

Eddev
Eddev

Reputation: 900

You've set i = 0 inside the for loop. Instead, set it outside and increment.

Upvotes: 0

EbraHim
EbraHim

Reputation: 2359

Use this program instead:

nl=[]
for x in range(1500, 2700):
    if (x%7==0) and (x%5==0):
        nl.append(x)
print nl

It works as below:

>>> ================================ RESTART ================================
>>> 
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995, 2030, 2065, 2100, 2135, 2170, 2205, 2240, 2275, 2310, 2345, 2380, 2415, 2450, 2485, 2520, 2555, 2590, 2625, 2660, 2695]
>>> 

Problems of your code:

  1. You are trying to assign values to a zero-length list (i.e. to nl). You must use append() method to append its length.
  2. You want to join integer numbers in a list with , character. You don't need it at all. print the list itself. :)

Upvotes: 0

sberry
sberry

Reputation: 132138

You cannot set arbitrary indexes into a 0 length list.

Use a list, and append:

# nl[i]=x <- this won't work
nl.append(x)

Since you don't seem to use the actual index when you print, this should work. Be advised, you will need to use

print(",".join(map(str, nl)))

as join won't work on ints.

If you do need the index, I would suggest using a dictionary.

nl = {}
low = 1500 
high = 2700
for x in range(low, high):
     if (x % 7 == 0) and (x % 5 ==0):
         nl[x-low] = x
print(','.join(map(str, nl.values())))

NOTE the values in this case won't be sorted. If you want them to be then you could sort them

print(','.join(map(str, sorted(nl.values()))))

Also, you can now iterate the indexes and values (even in sorted order) with

for index, value in sorted(nl.items()):

Upvotes: 0

Julien Spronck
Julien Spronck

Reputation: 15433

In addition to the zero-length list problem mentioned in other answers. You set i = 0 at every iteration of your loop. So you always change the same element (the first one) of your list.

Upvotes: 1

Strik3r
Strik3r

Reputation: 1057

Instead of using a index you can try using the append method in list which would be really helpful in this case . Try the following

nl=[]
for x in range(1500,2700):
if x%7==0 and x%5==0 :
    m.append(x)
print(nl)

Hope it Helps.

Upvotes: 0

Mehdi
Mehdi

Reputation: 93

nl=[] 
for x in range(1500, 2700):

    if (x%7==0) and (x%5==0):
       nl.append(str(x))

print (','.join(nl))

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168866

You defined n to be a zero-length list, but you write to (presumed) elements beyond the end of the list.

One way to fix this is to change the assignment of n to:

n = [None]*1200

Upvotes: 0

Related Questions