[英]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.