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//////

1 Answers1

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()
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 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? – lostinlearning-sation Apr 25 '15 at 20:56
  • ranges start at 0 if you don't provide a start, 0 * anything is 0 – Padraic Cunningham Apr 25 '15 at 20:59
  • 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? – lostinlearning-sation Apr 25 '15 at 21:05
  • 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 – Padraic Cunningham Apr 25 '15 at 21:06
  • 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 – lostinlearning-sation Apr 25 '15 at 21:09
  • I think this is a really good resource, it covers everything from the very basics to slightly more advanced http://anandology.com/python-practice-book/ – Padraic Cunningham Apr 25 '15 at 21:12
  • When I try running the for loop without your .format way of outputting I'm not understanding why I simply can't get any output. for i in range(1, hours + 1): distanceTraveled=0 distanceTraveled = speedMPH * i print(hours + distanceTraveled) – lostinlearning-sation Apr 25 '15 at 21:47
  • There is no reason you would not get output, is your print inside the loop? – Padraic Cunningham Apr 25 '15 at 21:48
  • Yes, I updated the original post to show what I'm currently using. I'm hoping to grasp this then learn your new & far superior .format method. – lostinlearning-sation Apr 25 '15 at 21:50
  • lol, you are using `0` from the hours variable outside the function so you never loop. Move the code inside the function as I have – Padraic Cunningham Apr 25 '15 at 21:53
  • Hmmm so there's something with scopes of variables that I'm not understanding either. Sigh. Thank you very much for your critiques! I'm going to fiddle with this a little to see if I can get the output to output in the columns that I laid out in line 25 of my code. – lostinlearning-sation Apr 25 '15 at 22:02
  • yes, the hours you are accessing is the global hours while the one in the function is local to the function. – Padraic Cunningham Apr 25 '15 at 22:06
  • OMG it's working the way I had envisioned! Thank you so very much Padraic Cunningham! Thank you! Thank YOU! – lostinlearning-sation Apr 25 '15 at 22:07
  • So if I had wanted to make it work the way I had it in two functions I would have had to be properly passing hours so it'd be something like the below chunk. def userInput(hours): 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.")) – lostinlearning-sation Apr 25 '15 at 22:08
  • The hours outside would never change, ints are immutable any += etc creates a new object, you also reassign hours to the input so even if it were possible you would have already rebound hours to a new object – Padraic Cunningham Apr 25 '15 at 22:11
  • Sigh. Good to know. I found the below forum thread which gives a good explanation furthering on your answer. Looks like I will need to do python coding where if the value is to be used somewhere all relevant code that needs the variable should be included in that function. Thank you very much Padraic. http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – lostinlearning-sation Apr 25 '15 at 22:22
  • You're welcome. You can also return the variables and pass the return values to other functions . – Padraic Cunningham Apr 25 '15 at 22:24