简体   繁体   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))  

I keep getting this error message:我不断收到此错误消息:

'int' object has no attribute 'rjust'

... for the last line ...最后一行

I think It is because you are trying to rjust an int variable.我认为这是因为您正在尝试对 int 变量进行调整。

Add str() around the things you are trying to rjust:在您尝试调整的内容周围添加 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))

That should work.那应该工作。

The error is saying that the variable "v" is an integer.错误是说变量“v”是一个整数。 You can cast "v" to a string, example您可以将“v”转换为字符串,例如

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

This will ensure that no matter if either k or v is an int, it will always be converted to a string and print as normal.这将确保无论 k 或 v 是否为 int,它始终会被转换为字符串并正常打印。

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

相关问题 我写了一段代码,但在编辑器中使用 matplotlib 时出现错误我收到无效语法错误,有人知道为什么吗? - I've write a code and I'm getting error while using matplotlib in the editor I am getting invalid syntax error, does anyone know why? 有谁知道为什么我在使用多处理时遇到这个基本错误 - Does anyone know why i am getting this basic error when using multiprocessing 有人知道我为什么要得到“退出代码为0的进程完成”吗? - Anyone know why I'm getting a “Process finished with exit code 0”? 我不知道为什么我不断收到错误 'str' has not attribute 'message' - I don't know why I keep getting the error 'str' has not attribute 'message' 我收到一个导入错误,有人知道解决方案吗? - I'm getting an import error, does anyone know the solution? 为什么我不断收到此错误消息? - Why do i keep getting this error message? 我一直收到这个错误,我不知道为什么 - i keep getting this error and i don't know why 为什么我在尝试运行代码时不断收到导入错误消息? - Why do I keep getting a error message for import when I try to run my code? 我不断收到类型错误,不知道为什么 - I keep getting a type error and don't know why 有谁知道为什么我没有得到输出,代码确实编译但不返回任何输出 - Does anyone know why i do not get an output, the code does compile but returns no output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM