0

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))
Kasravnd
108k19 gold badges167 silver badges195 bronze badges
asked Apr 21, 2016 at 5:54

8 Answers 8

1

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.

answered Apr 21, 2016 at 6:03
Sign up to request clarification or add additional context in comments.

Comments

0

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
answered Apr 21, 2016 at 5:59

Comments

0
nl=[] 
for x in range(1500, 2700):
 if (x%7==0) and (x%5==0):
 nl.append(str(x))
print (','.join(nl))
answered Apr 21, 2016 at 6:00

Comments

0

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.

answered Apr 21, 2016 at 6:01

Comments

0

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()):
answered Apr 21, 2016 at 5:59

Comments

0

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. :)
answered Apr 21, 2016 at 6:04

Comments

0

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

answered Apr 21, 2016 at 6:06

Comments

0

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))) 
answered Apr 21, 2016 at 6:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.