我試圖從一個包含城市名稱,里程錶讀數文件中讀取,以及多少燃料離開了。該文本文件的內容是:分配的從文件讀取,存儲的值,數學運算
Kennewick 97840.4 40.0<br>
Benton_City 97861.3 37.6<br>
Prosser 97878.2 35.9<br>
Grandview 97886.5 35.0<br>
Sunnyside 97894.6 34.1<br>
Granger 97904.6 33.1<br>
Toppenish 97913.0 32.2<br>
Wapato 97921.7 31.3<br>
Union_Gap 97931.6 30.4<br>
Yakima 97935.2 30.0<br>
Selah 97939.4 29.6<br>
Ellensburg 97972.7 26.0<br>
Thorp 97981.1 25.1<br>
Rosyln 98002.8 22.6<br>
Easton 98016.1 21.1<br>
North_Bend 98058.1 16.9<br>
Preston 98067.7 16.0<br>
Issaquah 98073.6 15.4<br>
East_Gate 98082.9 14.4<br>
Mercer_Island 98088.5 13.7<br>
Seattle 98095.4 13.0<br>
一部分減去當前從最後通讀(均存儲在變量英里這些值)通讀。
def BusRoute():
path = "C:\\"
ext = ".txt"
lines = ""
stopname = ""
miles = 0
prevmiles = 0
milest = 0
fuel = 0.0
mpg = 0.0
i = 0
filename = input("Please enter the file name. The file path [C:\...] and extension [*.txt] is not required: ")
inFile = open(path + filename + ext, "r")
for lines in inFile:
stopname, miles, fuel = lines.split(" ")
miles = float(miles)
fuel = float(fuel)
prevmiles = miles
milest = miles - prevmiles
miles += 1
print("Since last stop:", float(milest), "miles")
我的問題是,我從miles
當前讀數努力的miles
最後讀值存儲在一個名爲prevmiles
變量,然後減去prevmiles
(最後里程讀取),但它一直減當前miles
從當前miles
,顯然使我想打印出的值0.0。
非常感謝。此代碼工作;我所做的只是修改了一點,以便爲我的其他程序提供一致的格式。 – html92