user9367977
user9367977

Reputation:

Iterating for loop Python

This is the problem: Write a program named q1() that prompts the user for the starting and ending distance in Kilometers (km), and an increment value. It should then print the conversion table showing values for Kilometers, Miles (M) and Feet (ft). Each value should be displayed as they are calculated.

this is my code:

 print("This program prints a translation table between the Kilometers (km), Miles (M) and Feet (ft) scales.")

start_km = input("Enter the Starting Kilometers (km): ")
start_km = int(start_km)
end_km = input("Enter Ending Kilometers (km): " )
end_km = int(end_km)
increment = input("Increment value: ")
increment = int(increment)
km = start_km
M = km*(.621371)
ft = M*(5280)

print("km", "   ", "M", "   ", "ft")
print("==========================")
list = ['km', 'M', 'ft']
for i in range(start_km,end_km+1,increment):
    km = start_km
    M = km*(.621371)
    ft = M*(5280)
    print( km, "    ", M, "    ", ft)

my problem is that i am getting a list but it is repeating the input value and not incrementing the calculation. how do i get it to iterate ?

Upvotes: 2

Views: 178

Answers (2)

Jithin Scaria
Jithin Scaria

Reputation: 1309

It should be,

km = i

inside loop not,

km = start_km

Upvotes: 0

FlyingTeller
FlyingTeller

Reputation: 20472

for i in range(start_km,end_km+1,increment)

does not change the value of start_km, so you are just printing the same value again and again. The for loop changes the value of i, starting at start_km and ending at end_km, so you should do:

for km in range(start_km,end_km+1,increment):
    M = km*(.621371)
    ft = M*(5280)
    print( km, "    ", M, "    ", ft)

which increments km automatically, just like you wanted

Upvotes: 2

Related Questions