![](/img/trans.png)
[英]Python - slice indices must be integers or None or have an __index__ method
[英]Slice indices must be integers or None or have an __index__ method error?
所以我正在嘗試更正此代碼:
if len(username) < 5 or username.index("!", "@") == -1:
print("no")
else:
print(f"Your username is {username}")
但是,我收到有關切片索引需要為整數的錯誤消息。 我只是不知道該怎么做。 基本上我的代碼是檢查用戶名。
如果長度小於 5 個字符或如果以下符號不包括拒絕嘗試等。
對你起作用嗎:
if len(username) < 5 or not set(username) & set(["!", "@"]):
print("no")
else:
print(f"Your username is {username}")
str.index(str, beg = 0 end = len(string))
在您的通話中:
username.index("!", "@")
beg
的值將設置為'@'
- 這不是一個有效的索引。 這就是錯誤告訴你的。
您的支票可以簡單地是:
if len(username) < 5 or not ('!' in username or '@' in username):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.