2

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 ?

1
  • 1
    You keep resetting km to start_km inside the loop, i.e. km = start_km. You need to increment it... Commented Feb 16, 2018 at 7:17

2 Answers 2

2
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

Sign up to request clarification or add additional context in comments.

2 Comments

this was super helpful and easy to understand. Thanks so much , really helped!!
@MinnaAdelRubio always glad to help
0

It should be,

km = i

inside loop not,

km = start_km

Comments

Your Answer

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