Reputation:
So I am trying to correct this code:
if len(username) < 5 or username.index("!", "@") == -1:
print("no")
else:
print(f"Your username is {username}")
However I get the error message about slice indices needing to be integers. I am just not sure how to do that. Basically my code is checking a username.
if the length is less than 5 characters OR if the following symbols not included reject the attempt etc.
Upvotes: 0
Views: 395
Reputation: 21275
Check the signature of the str.index
method
str.index(str, beg = 0 end = len(string))
In your call:
username.index("!", "@")
The value for beg
will be set to '@'
- which is not a valid index. That is what the error is telling you.
Your check can simply be:
if len(username) < 5 or not ('!' in username or '@' in username):
Upvotes: 1
Reputation: 2167
Does it work for you:
if len(username) < 5 or not set(username) & set(["!", "@"]):
print("no")
else:
print(f"Your username is {username}")
Upvotes: 1