2011-10-17 96 views
1

我試圖從一個包含城市名稱,里程錶讀數文件中讀取,以及多少燃料離開了。該文本文件的內容是:分配的從文件讀取,存儲的值,數學運算

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。

回答

1

兩件事情會在這裏:

  1. 您需要初始化stopnameprevmilesfuel與文件的第一行,並
  2. 需要交換的兩行,你分配milesprevmiles

下面是更新的功能:

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 (no extension)").strip() 

    inFile = open(path + filename + ext, "r") 

    stopname, prevmiles, fuel = inFile.readline().split() 
    prevmiles = float(prevmiles) 
    fuel = float(fuel) 

    for lines in inFile: 
     stopname, miles, fuel = lines.split(" ") 

     miles = float(miles) 
     fuel = float(fuel) 

     milest = miles - prevmiles 

     prevmiles = miles 

     miles += 1 

     print("Since last stop:", float(milest), "miles") 
+0

非常感謝。此代碼工作;我所做的只是修改了一點,以便爲我的其他程序提供一致的格式。 – html92

2

你有

prevmiles = miles 

milest = miles - prevmiles 

這保證milest將爲0儘量只交換這兩行的順序。

+0

所以,我想那剛纔和它輸出的第一個「自最後一站:-97840.4英里」,仍然對打印其餘0.0。 – html92

0

你應該只改變操作的順序。首先進行減法,然後爲prevmiles分配一個值。

+0

我認爲你所說的與其他人的回答相似,但它並不像我需要的那樣工作。首先,它顯示「自上次停車後:-97840.4英里」,並且仍然打印0.0。 – html92

0

我換了兩行,並得到了我想你正在尋找。

prevmiles = miles 

milest = miles - prevmiles 

注意 - 此代碼是爲Python 2.5.4,所以在幾部分,因爲
上線的末端,而且語法變化爲燃料的花車,所以我評論說出來給了錯誤。

def BusRoute(): 
    path = "C:\\" 
    ext = ".txt" 

    lines = "" 
    stopname = "" 
    miles = 0 
    prevmiles = 0 
    milest = 0 
    fuel = 0.0 
    mpg = 0.0 
    i = 0 

    filename = raw_input("Please enter the file name. The file path [C:\...] and extension [*.txt] is not required: ") #raw_input for text in Python 2.5 

    inFile = open(path + filename + ext, "r") 

    for lines in inFile: 
     stopname, miles, fuel = lines.split(" ") 

     miles = float(miles) 
     ##fuel = float(fuel) (Gives me an error message) 

     milest = miles - prevmiles 

     prevmiles = miles    

     miles += 1 

     print "Since last stop:", float(milest), "miles" #Print syntax is different in Python 2.5 

BusRoute() 

給人的輸出:

Please enter the file name. The file path [C:\...] and extension [*.txt] is not required: name 
Since last stop: 97840.4 miles 
Since last stop: 20.9 miles 
Since last stop: 16.9 miles 
Since last stop: 8.3 miles 
Since last stop: 8.10000000001 miles 
Since last stop: 10.0 miles 
Since last stop: 8.39999999999 miles 
Since last stop: 8.7 miles 
Since last stop: 9.90000000001 miles 
Since last stop: 3.59999999999 miles 
Since last stop: 4.2 miles 
Since last stop: 33.3 miles 
Since last stop: 8.40000000001 miles 
Since last stop: 21.7 miles 
Since last stop: 13.3 miles 
Since last stop: 42.0 miles 
Since last stop: 9.59999999999 miles 
Since last stop: 5.90000000001 miles 
Since last stop: 9.29999999999 miles 
Since last stop: 5.60000000001 miles 
Since last stop: 6.89999999999 miles 

你爲什麼不嘗試從文件打印英里的循環,以確保您得到正確的價值觀?

+0

不完全正確 - 第一行仍然是錯誤的。 –

+0

@Ehan怎麼這樣?如果他想讓第一行打印0英里,那麼他需要使用一組不同的邏輯。現在看來,如果他在開始時將英里數設置爲0,那麼第一個輸出將是97840.4 –

+0

。在他的其中一個評論中,他表示......哦,-97840。4 - 猜我應該打掃我的眼鏡。撤消downvote。抱歉! –