python - 为什么我收到此错误?TypeError:“列表”对象不能解释为整数
问题描述
我的代码中出现以下错误我做错了什么:
Traceback (most recent call last):
File "Solution.py", line 15, in <module>
print_formatted(n)
File "Solution.py", line 9, in print_formatted
print(d.ljust([2,' ']),o.ljust([2,' ']),h.ljust([2,' ']),b.ljust([2,' ']))
TypeError: 'list' object cannot be interpreted as an integer
TypeError: 'list' object cannot be interpreted as an integer
代码如下所示:
def print_formatted(number):
for i in range(1,number+1):
d=str(i)
o=str(oct(i))[2:]
h=(str(hex(i))[2:]).capitalize()
b=str(bin(i))[2:]
print(d.ljust([2,' ']),o.ljust([2,' ']),h.ljust([2,' ']),b.ljust([2,' ']))
if __name__ == '__main__':
n = int(input())
print_formatted(n)
解决方案
ljust
接受两个参数,而不是两个元素的列表。用这个:
print(d.ljust(2, ' '), o.ljust(2, ' '), h.ljust(2, ' '), b.ljust(2, ' '))
推荐阅读
- sql-server - 如何从此结果集中获取缺失的数据?
- prolog - 带参数的 Prolog DCG
- r - 带有 MLR 的 makeClassif - ID 列从任务中排除
- scrapy - 使用扩展发送带有抓取数据的电子邮件
- mysql - 如何找到我与其他用户的距离?
- powershell - 从 Active Directory 为用户读取组时,“GetAuthorizationGroups”引发异常
- javascript - 如何只用 js 切换一次类
- javascript - 在 Java Nashorn 中使用 JSON.parse
- laravel - Laravel Eleqent 如何在 Postgresql 中使用 DATE_FORMAT
- python-3.x - 解压 .zip 文件并使用 python 和 boto 3 传输到 s3 存储桶