无法在 python 中创建工作循环

cannot create a working loop in python

我对编程逻辑还很陌生,所以我不确定我是否可以正确地表达这一点。我正在使用 Python 2.7 并尝试编写一个脚本,该脚本将重复直到输入零。我试过 if、else 和 while 语句,得出的结论是我对逻辑的了解还不够,无法了解 Python。例如...我很新,init 对我来说毫无意义。我几乎在收到的每个搜索结果中都看到过这个短语,但我不知道它是什么意思或是什么。我所在的 class 是逻辑 class 而不是 Python class。我可以用伪代码编写它,但我真的很想看到一个工作模型。请帮忙。此脚本运行完毕,输入零时退出,但不会再次提示行驶里程。

#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"
getMiles = float(input ('Enter Miles: '))
while getMiles == 0:
    print "END OF PROGRAM"
    exit
fuelEcon = getMiles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = getMiles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = getMiles * fuelIncrease
while costOfTrip == float:
    getMiles
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

我忘记提及的是强制性的 "END OF PROGRAM" 声明。我结合使用了您的答案,这很有效。再次感谢大家。我可以停止用头撞墙了。

#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or enter zero to quit"
getMiles = float(raw_input ('Enter Miles: '))
while getMiles >= 0:
    if getMiles == 0:
        print "END OF PROGRAM"
        exit()
    fuelEcon = getMiles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = getMiles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = getMiles * fuelIncrease
    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
    getMiles = float(raw_input ('Enter Miles: '))

Python 与伪代码相去不远,确实这里的问题不是代码而是逻辑。

要获得基本的"loop until entering zero"你可以有以下逻辑:

miles = -1
while miles != 0:
    miles = float(raw_input ('Enter Miles: '))

至于你自己的代码,当你的意思是 'if' 时,你似乎在使用 'while' 在第二个,你实际上只是命名一个什么都不做的变量(getMiles)

整个代码可能如下所示:

miles = float(raw_input ('Enter Miles: '))
while miles != 0:
    fuelEcon = miles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = miles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = miles * fuelIncrease

    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

    miles = float(raw_input ('Enter Miles: '))

** 不需要像其他人建议的那样使用 "while true",这从来都不是一件好事。

更高级的版本是提取可重复且独立于函数的逻辑部分

def trip_cost(miles):
    if(miles == 0):
        return False
    fuelEcon = miles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = miles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = miles * fuelIncrease
    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

    return True

while trip_cost(float(raw_input ('Enter Miles: '))):
    pass

至于 init 是什么,那是面向对象的更高级的话题,您现在可能还不应该担心

你很接近,这里是固定版本:

print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"
while True:
    getMiles = float(input ('Enter Miles: '))
    if getMiles == 0:
        print "END OF PROGRAM"
        exit()
    fuelEcon = getMiles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = getMiles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = getMiles * fuelIncrease
    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

我在整个代码块周围添加了一个 while True,这将导致问题被反复(永远)询问,直到用户输入 0 英里。

唯一需要修复的另一件事是 exit 是一个函数调用,因此它应该是 exit()

你可以这样做

#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"

while True:
    getMiles = float(input ('Enter Miles: '))
    if getMiles == 0:
        print "END OF PROGRAM"
        break

    print 'Do the other calculations'

进入无限循环,直到输入 0,此时跳出循环,程序结束。

您可以在 python 2.7 下使用 While 1: 以获得更快的性能,但我怀疑您目前会担心这个问题。

total_miles = 0

打印"To Calculate the cost of your trip,"

打印"enter the miles driven or zero to quit"

getMiles = float(输入('Enter Miles: '))

while getMiles != 0:

total_miles = getMiles + total_miles
getMiles = float(input ('Enter Miles: '))

其他:

print "END OF PROGRAM"
exit

fuelEcon = total_miles / 20

fuelCost = float(输入('Enter Cost of Fuel: $'))

costOfTrip = total_miles * fuelCost

fuelIncrease = (fuelCost * .1) + fuelCost

futureTrip = total_miles * fuelIncrease

而 costOfTrip == 浮动:

getMiles

打印"Cost of Trip: $",costOfTrip

打印"Cost of Trip With 10% Increase in Fuel Cost: $"、futureTrip

我只想说,就风格而言,即使是您的新循环条件/逻辑也可以快速整理以供阅读

print "To Calculate the cost of your trip,"
print "enter the miles driven or enter zero to quit"
getMiles = float(raw_input ('Enter Miles: '))
while getMiles != 0:
    fuelEcon = getMiles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = getMiles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = getMiles * fuelIncrease
    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
    getMiles = float(raw_input ('Enter Miles: '))
print "END OF PROGRAM"
# should be no need for exit() unless code is included in a subroutine