Python代码运行报'int' object has no attribute 'rjust'错误原因求助
报错原因
rjust()是Python字符串类型的专属方法,你传入的字典中,value值为整数(int)类型,直接对整数调用rjust()方法就会触发该报错。
修复方案
把代码中调用rjust()的部分先做字符串类型转换即可,修改后的代码如下:
def show_total_rooms_and_rates(dictionary): print('All the rooms and the rate you just checked') for k,v in dictionary.items(): print(k.ljust(30,'.')+str(v).rjust(10))
如果需要兼容key也可能是非字符串的场景,可以统一对key和value都做类型转换:
def show_total_rooms_and_rates(dictionary): print('All the rooms and the rate you just checked') for k,v in dictionary.items(): print(str(k).ljust(30,'.')+str(v).rjust(10))
内容的提问来源于stack exchange,提问作者Boy
相关产品推荐
相关产品推荐

