Reputation: 65
I am trying to read in from a file that contains city names, an odometer reading, and how much fuel is left. The contents of the text file are:
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>
Part of the assignment is subtracting the current read-through from the last read-through (both of those values stored in the variable miles).
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")
My problem is, I am trying to store the last-read value of miles
in a variable called prevmiles
, and then subtract prevmiles
(the last mileage read) from the CURRENT reading of miles
, but it keeps subtracting the current miles
from the current miles
, obviously making the value that I want printed out 0.0.
Upvotes: 1
Views: 445
Reputation: 65
I switched the two lines and got what I think you are looking for.
prevmiles = miles
milest = miles - prevmiles
Note - This code is for Python 2.5.4, so the syntax changes in a few parts, and because of the
on the end of the line the floats for fuel were giving errors so I commented that out.
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()
Gives the output:
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
Why don't you try to print the miles from the file in the loop to make sure you are getting the right values?
Upvotes: 0
Reputation: 69041
Two things going on here:
stopname
, prevmiles
, and fuel
with the first line of the file, andmiles
to prevmiles
.Here's the updated function:
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")
Upvotes: 1
Reputation: 716
You should just change the order of the operations. Do the substraction first and then assign a value to prevmiles.
Upvotes: 0
Reputation: 2323
You have
prevmiles = miles
milest = miles - prevmiles
which guarantees that milest will be 0. Try just switching the order of those two lines.
Upvotes: 2