Kasper Kasper - 1 year ago 95
Python Question

Python: range doesn't increase

I have to write a program that shows a column of kilograms and a column of pounds, starting at 1 kilogram and ending 99, increasing every step with 2.

I have the following code, and the range() works for the pounds part, but not for the kilograms part. It stays always 1 for the kilograms.

for k in range(1,3,1):
print("Kilograms","Pounds",sep=" ")
for i in range(1,101,2):
for j in range(2,223,3):
print(i,"",j,sep=" ")

Also, why can't I use floats in the range, because officially it is 2.2 pounds.

Thanks in advance!

Answer Source

Because you use break with in a loop.

In python you don't end a loop with anything but a decreased indentation. Remove your break statements and try again.

The break statements ends the current loop unconditionally. For example,

s = 0
for i in range(1, 101):
    s = s + i

will make s equal to 5050. However, if you break it some where, like

s = 0
for i in range(1, 101):
    s = s + i
    if i == 5:

s will stop increasing on 15.

As commentors say, you should learn the basics of python from some tutorial. There are pretty many free tutorials on the internet. Don't haste.

Besides, if you wanna use float steps in ranges, take a look at this answer; or rather, see comments below for a simple answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download