繁体   English   中英

我不断收到此代码的错误消息,有人知道为什么吗?

[英]I keep getting an error message with this code, does anyone know why?

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,'.')+v.rjust(10))  

我不断收到此错误消息:

'int' object has no attribute 'rjust'

...最后一行

我认为这是因为您正在尝试对 int 变量进行调整。

在您尝试调整的内容周围添加 str() :

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))

那应该工作。

错误是说变量“v”是一个整数。 您可以将“v”转换为字符串,例如

print(str(k).ljust(30,'.') + str(v).rjust(10))

这将确保无论 k 或 v 是否为 int,它始终会被转换为字符串并正常打印。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM