2

I'm trying to learn Python between self thought of projects relevant to me and utilizing teamtreehouse though it's slow progress.

I'm trying to find a tutorial on how to make a python 3.3.2 for loop run from a value of 0 until the value the user inputs into variable hours. So far I just get an error running this code. I'm not having success finding tutorials that cover this approach.

The below tutorial seems to cover starting at nothing then running through printing out values of lists/dictionariies http://www.python-course.eu/python3_for_loop.php

Same thing with this tutorial http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python_3/For_Loops

This has got me thinking if it's not possible and instead I need to research/learn other loops?

#//////MAIN PROGRAM START//////

#//////VARIABLE DECLARATION//////
speedMPH=0
timeTraveling=0
hours=1
distanceTraveled=0
#//////VARIABLE DECLARATION//////

#//////USER INPUT FUNCTION//////
def userInput():
    speedMPH=int(input("Please provide the speed the vehicle was going in MPH."))
    hours=int(input("Please provide the number of hours it has been traveling in hours."))
    #////////////////testing variable values correct////////////////
#    print(speedMPH)
#    print(hours)
#    print(distanceTraveled)
    #////////////////testing variable values correct////////////////
#//////USER INPUT FUNCTION//////
    print('Distance Traveled\t\t\t' + 'Hours')
    for i in range(1, hours + 1):
        distanceTraveled=0
        distanceTraveled = speedMPH * i
        print(distanceTraveled, '\t\t\t\t\t', i)
#//////CALLING FUNCTION//////
userInput()
#//////CALLING FUNCTION//////
3
  • Sorry Padraic I don't know how I forgot the most important piece. I edited it to include that. Commented Apr 25, 2015 at 20:44
  • where are speed and time supposed to come from? Commented Apr 25, 2015 at 20:48
  • Thank you Padraic for correcting me that I had my variables/syntax wrong. I've updated that now. I'm still having an issue with running the for loop though. Commented Apr 25, 2015 at 20:53

1 Answer 1

1

Not totally sure what it is you are trying to do but using range and keeping you code to a single function will be a lot closer:

def user_input():
    # keep track of running total 
    total_distance = 0
    # cast hours and mph to int 
    speed_mph = int(input("Please provide the speed the vehicle was going in MPH."))
    hours = int(input("Please provide the number of hours it has been traveling in hours."))
    # loop from 1 to hours + 1, ranges are not inclusive
    for i in range(1, hours + 1):
        distance_traveled = speed_mph * i
        total_distance += distance_traveled 
        print("Travelled {} miles after {} hour/s".format( distance_traveled,i))

    print("Total distance travelled {} miles after {} hour/s".format(total_distance,hours))
user_input()
Sign up to request clarification or add additional context in comments.

17 Comments

So I have to put in range(1,hours+1) Somewhere in my reading/tutorials I was under the impression for loops automatically incremented whatever counter variable they were given by +1?
ranges start at 0 if you don't provide a start, 0 * anything is 0
Ok that's a critical piece of knowledge for me. Thank you Padraic. Do the for loops automatically increase the counter by 1 each iteration or is it necessary to include the hours+1?
hours + 1 just means we loop up to including hours. We want to include hours in the loop, unless you supply a third argument to range which is the step the default is 1
Wow ouch that shattered my logic of things. I need to go practice that and different for loop uses. I was of a whole different understanding where I could do for (variable) <= (variable #2) Statements >>output
|

Your Answer

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.