简体   繁体   中英

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.

Add str() around the things you are trying to 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(str(k).ljust(30,'.')+str(v).rjust(10))

That should work.

The error is saying that the variable "v" is an integer. You can cast "v" to a string, example

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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