因此,如果地形编号小于或等于洪水等级编号,则此代码假设显示“*”。如果它更大,则在矩阵中替换“”。
我认为输出看起来像这样:
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
* * *
* * * *
* * * * * *
* * * * * *
* * * * * *
* * * * *
* * *
由于PDF所带来的传输问题,间距可能稍微偏差。
我的输出如下:
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
* * * * * * *
* * * * * * *
* * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
*
*
* *
*
* * *
* * * *
* * * *
* * *
* *
* * *
* *
正如你所看到的,我的星号太多了。我知道问题出在其中一个循环中,但我不知道在哪里。我正在思考我的主要功能。输出应该是什么在从PDF文本的转移中非常混乱,正如你可以在高程100和105看到它非常混乱。 95是完美的,服务器就是最好的例子。
这是我的代码,任何帮助都将超过赞赏。
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()
答案 0 :(得分:1)
您正在进行字符串比较而不是整数。确保在从文件读取时将数字转换为整数。
字符串&#34; 100&#34;小于&#34; 95&#34;这就是为什么你会得到一个&#34; *&#34;那里。
您可以修复:
来自readTerrain()
功能:
line = line.split(" ")
变为
line = map(int, line.split(" "))
来自floodLevels()
函数:
line = line.rstrip()
变为
line = int(line.rstrip())
由于值现在是int而不是字符串,因此您需要将它们转换回来或使用不同的字符串格式。最简单的改变就是转换回str以保持间距。
这意味着更改printTerrain(matrix)
中的行:
print("%5s" % matrix[i][j], end="")
要
print("%5s" % str(matrix[i][j]), end="")
那应该输出正确的东西。你的循环看起来是正确的。