簡體   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