这是我的代码:
def PopDensity(population, area):
PopulationDensity = (population/area)
return PopulationDensity
state= "Maryland"
population='6,052,000'
area="12,407"
PopDensity(int(population), int(area))
stat="The population density of %s is %s."
print(stat % (state,PopulationDensity))
Python总是在上面的print函数中向我返回错误的内容。看起来是这样的:
Traceback (most recent call last): File "C:/Users/adamn/OneDrive/Desktop/.py files/Assignment5_1astrub1359960.py", line 8, in PopDensity(int(population), int(area)) ValueError: invalid literal for int() with base 10: '6,052,000'"
请告诉我打印功能有什么问题,并提出改进建议。我看了另一个谷歌向我提出的问题,但它并没有真正起到帮助作用
问题在于传递给
int()
的数字格式。您可以直接初始化整数,例如population = 6052000
。您使用的是字符串而不是整数。相应地修改代码会产生相关问题 更多 >
编程相关推荐