-2

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="        ")
       break
    break

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

Thanks in advance!

CoderKasper
  • 307
  • 1
  • 2
  • 8
  • 3
    What are the breaks for? Break statements will end a loop. Also, range uses integers, which is why you can't use floats. – numbermaniac Sep 28 '16 at 02:14
  • 1
    First, learn the basics of python loops. Second, once you do that, try solving your problem yourself, and I think you will succeed. Then if you still need help, come back here and edit your question and show a few rows of your expected output. – Sнаđошƒаӽ Sep 28 '16 at 02:21

3 Answers3

2

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:
        break

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.

Community
  • 1
  • 1
1

First off, remove the breaks, as those will prematurely end the loops iterations. Secondly, why are you using nested forloops?

For what you described, nested loops are not even required. You simply need to use one forloop. Use range() once, to step through the values 1 to 99 in increments of 2.

From you description, something such as this should suffice:

for i in range(1, 100, 2): # for the numbers 1-99, going by twos
    print('pounds: {} kilograms: {}'.format(i, i * 2.2)) # print pounds, print kilograms

You seem to be confused about loops in python and he builtin range() function. I recommend looking at the official Python documentation for both:

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
0

Try this

import numpy as np


KG_TO_LBS = 2.20462
KG = np.arange(0,100,2.20462)

print("Kilograms", "Pounds")
for kg in KG:
    print(kg, kg/KG_TO_LBS)

you may have to change the printout to the format you like.

mengg
  • 310
  • 2
  • 9