You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 22:24:03