【发布时间】:2017-08-20 19:31:24
【问题描述】:
我怎样才能使这条线工作? print('州平均人口:', totalPop/50)
它不会让我,因为 50 是一个 int。
def main ():
file = open ('StateCensus2010.txt', 'r')
name = file.readline()
abb = file.readline()
pop = file.readline()
minPop = pop
minName = name
maxPop = pop
maxName = name
totalPop = pop
totalPop += pop
for state in range (49):
name = file.readline()
abb = file.readline()
pop = file.readline()
if pop < minPop:
minPop = pop
minName = name
if pop > maxPop:
maxPop = pop
minName = name
print(' State with MAX population:',maxName, maxPop)
print('State with MIN population:', minName, minPop)
print('Average state population:', totalPop/50)
【问题讨论】:
-
你能展示你的完整代码吗?即什么是 totalPop?
-
究竟是什么错误?发布完整的回溯。
-
当
totalPop是一个整数时效果很好。请发布实际的错误消息,包括完整的回溯。 -
TypeError: unsupported operand type(s) for /: 'str' and 'int' 是回溯。刚刚包含完整的代码
-
pop是一个字符串,所以totalPop也是一个字符串。