![](/img/trans.png)
[英]TypeError: unsupported operand type(s) for <<: 'str' and 'int'
[英]TypeError: unsupported operand type(s) for -: 'str' and 'int' [Python]
為什么這個錯誤一直出現? 我只是想創建一個表。 代碼如下:
import datetime
q= datetime.date(2004,12,25)
e= datetime.date(2019,11,23)
f= datetime.date(2019,11,26)
p= datetime.date(2004,12,13)
nam=[["attack on titan",10,"completed",p,q],["one punch man",10,"WATCHING",e,f]]
print("| NAME | SCORE | STATUS | DATE STARTED | DATE ENDED |")
for KI in nam:
print("|",KI[0]," "*22-len(KI[0]),"|"," ",KI[1]," ","|",KI[2]," "*(10-len(KI[2])),"|",""*2,KI[3],
" "*2,"|"," "*3,KI[4]," "*2,"|")
不要過多關注空間乘法。
錯誤:
| NAME | SCORE | STATUS | DATE STARTED | DATE ENDED |
Traceback (most recent call last):
line 9, in <module>
print("|",KI[0]," "*22-len(KI[0]),"|"," ",KI[1]," ","|",KI[2]," "*(10-len(KI[2])),"|"," "*2,KI[3]," "*2,"|"," "*3,KI[4]," "*2,"|")
TypeError: unsupported operand type(s) for -: 'str' and 'int'
.........................................................................................
錯誤發生在" "*22-len(KI[0])
。
" "*22
是一個由 22 個空格組成的字符串, len(KI[0])
是一個整數。
正如TypeError
告訴你的那樣,從字符串中減去一個整數是沒有意義的。
我猜你的意圖是" "*(22-len(KI[0]))
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.