Reputation: 11
So this code is suppose to display an "*" if the terrain number is less than or equal to the flood level number. If it is larger a " " is replaced in the matrix.
The output I am suppose to get looks like this:
100 104 107 103 109 106 112 115
102 101 105 100 106 110 115 120
103 99 102 96 101 105 110 122
97 94 98 100 104 100 109 113
94 93 95 98 100 103 108 110
97 99 101 101 104 108 110 115
99 101 104 107 110 110 115 125
Flooding at elevation = 95
*
* * *
Flooding at elevation = 100
*
*
* *
* * * * *
* * * * *
* *
*
Flooding at elevation = 105
* * *
* * * *
* * * * * *
* * * * * *
* * * * * *
* * * * *
* * *
The spacing might be a little off due to transfer issues with the PDF it is on.
My output looks like this:
100 104 107 103 109 106 112 115
102 101 105 100 106 110 115 120
103 99 102 96 101 105 110 122
97 94 98 100 104 100 109 113
94 93 95 98 100 103 108 110
97 99 101 101 104 108 110 115
99 101 104 107 110 110 115 125
Flood elevation is: 95
* * * * * * *
* * * * * * *
* * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
*
*
* *
*
* * *
* * * *
* * * *
* * *
* *
* * *
* *
Flood elevation is: 100
* * * * * * *
* * * * * * *
* * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
*
*
* *
*
* * *
* * * *
* * * *
* * *
* *
* * *
* *
Flood elevation is: 105
* * * * * * *
* * * * * * *
* * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
*
*
* *
*
* * *
* * * *
* * * *
* * *
* *
* * *
* *
As you can see, I am getting way too many asterisks. I know the problem is in one of the loops but I'm just not sure where. I was thinking in my main function. What the output SHOULD be was very messed up in the transfer from PDF text, as you can see at elevation 100 and 105 it was very messed up. 95 is perfect and servers as the best example.
This is my code, any help would be more than appreciated.
def main():
# Getting terrain from read terrain and flood elevations from flood levels
data = readTerrain()
floodElevation = floodLevels()
# Printing the matrix of terrain numbers
printTerrain(data)
# Making the matrix of "*" and " " ready to print
floodMatrix = floodMap(data, floodElevation)
# Move through the list of flood elevations and print each map for that number
for num in floodElevation:
print()
print("Flood elevation is: ", num)
print()
printFlood(floodMatrix)
# Def readTerrain
# @return returns the matrix of numbers. A list of lists.
def readTerrain():
inFile = open("terrain.txt", "r")
data = inFile.readlines()
matrix = []
for line in data:
matrix.append(line.rstrip().split(" "))
inFile.close()
return matrix
# Def printTerrain
# @param matrix - gets the matrix from readTerrain and prints the matrix.
# @return returns the printed matrix
def printTerrain(matrix):
for i in range(len(matrix)):
for j in range(len(matrix[0])):
print("%5s" % matrix[i][j], end="")
print()
return matrix
# Def floodMap
# @param matrix, passing the data in to compare the numbers.
# @param h2OElevation, passing in the flood levels to compare to the terrain levels
# @return map, returns the matrix of "*" and " "
def floodMap(matrix, h2OElevation):
map = []
for num in h2OElevation:
for i in range(len(matrix)):
row = []
for j in range(len(matrix)):
if matrix[i][j] <= num:
row.append("*")
else:
row.append(" ")
map.append(row)
return map
# Def floodLevels
# @return floodElevation, returns a list of numbers of the h2OElevations
def floodLevels():
inFile = open("flood.txt", "r")
elevation = inFile.readlines()
floodElevation = []
for line in elevation:
floodElevation.append(line.rstrip())
inFile.close()
return floodElevation
# Def printFlood
# @param floodMatrix, brings the floodMap of "*" and " " to print the matrix
# @return floodMatrix, returns the printed matrix of flooded areas
def printFlood(floodMatrix):
for i in range(len(floodMatrix)):
for j in range(len(floodMatrix[0])):
print("%5s" % floodMatrix[i][j], end="")
print()
return floodMatrix
# Call main
main()
Upvotes: 1
Views: 65
Reputation: 1216
You're doing string comparison instead of integer. Make sure to convert the numbers to ints when reading from file.
The string "100" is less than "95" which is why you get an "*" there.
Fixes you can make:
From the readTerrain()
function:
line = line.split(" ")
becomes
line = map(int, line.split(" "))
And from the floodLevels()
function:
line = line.rstrip()
Becomes
line = int(line.rstrip())
Since the values are now ints instead of strings, you'll need to convert them back or use a different string formatting. Easiest change would be to convert back to str to keep you spacing.
That means changing the line in printTerrain(matrix)
from:
print("%5s" % matrix[i][j], end="")
To
print("%5s" % str(matrix[i][j]), end="")
That should output the right thing. Your loops look correct.
Upvotes: 1