Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使循环运行直到达到变量值_Python_Loops_For Loop_Python 3.3 - Fatal编程技术网

Python 使循环运行直到达到变量值

Python 使循环运行直到达到变量值,python,loops,for-loop,python-3.3,Python,Loops,For Loop,Python 3.3,我试图在自我思考与我相关的项目和利用teamtreehouse之间学习Python,尽管这进展缓慢 我试图找到一个关于如何使Python3.3.2for循环从值0运行到用户输入到变量hours的值的教程。到目前为止,我只是在运行这段代码时遇到了一个错误。我没有成功地找到涵盖这种方法的教程 下面的教程似乎涵盖了从无到有,然后从头到尾打印列表/字典的值 本教程也是如此 为Python编写的教程/for循环 这让我思考,如果这不可能,我需要研究/学习其他循环 #//////MAIN PROGRAM

我试图在自我思考与我相关的项目和利用teamtreehouse之间学习Python,尽管这进展缓慢

我试图找到一个关于如何使Python3.3.2for循环从值0运行到用户输入到变量hours的值的教程。到目前为止,我只是在运行这段代码时遇到了一个错误。我没有成功地找到涵盖这种方法的教程

下面的教程似乎涵盖了从无到有,然后从头到尾打印列表/字典的值

本教程也是如此 为Python编写的教程/for循环

这让我思考,如果这不可能,我需要研究/学习其他循环

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

不完全确定您想做什么,但使用并将代码保持在单个函数将更接近:

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纠正我的变量/语法错误。我现在已经更新了。但是,我仍然在运行for循环时遇到问题。因此,我必须在阅读/教程中的某个地方设置范围(1,hours+1)。我觉得for循环会自动递增,无论它们由+1给出什么计数器变量?如果不提供起始值,范围从0开始,0*一切都很好,这对我来说是一个至关重要的知识。谢谢你,帕德雷克。for循环是否会在每次迭代时自动将计数器增加1,或者是否需要包含小时数+1?小时数+1只是意味着我们循环到包含小时数。我们希望在循环中包含小时,除非您为range提供第三个参数,这是一个默认步骤,它打破了我的逻辑。我需要去实践,不同的循环使用。我对(变量)>输出的理解完全不同