I have been making a python hash function ,and I have this so far.
keystring = [1, 4, 2, 3, 2, 1, 4, 1]
bucket1 = []
bucket2 = []
bucket3 = []
bucket4 = []
while "1".lower() in keystring:
if "1".lower() in keystring:
bucket1.append(keystring[keystring.index("1") 1])
print (bucket1)
I have tried running it but it just says nothing ,and quits the program immediately.
CodePudding user response:
while "1".lower() in keystring:
This is never true or, to better say, True
.
In Python "1"
is a string (str
), which has the .lower
method, which returns a string.
Since keystring
is a list
of int
egers, you will never find a str
in it, and the while
loop will never begin.
CodePudding user response:
As @FLAK-ZOSO said you are searching for str
in while
loop.
- You either can change the
keystring
items tostr
like this:
keystring = ["1", "4", "2", "3", "2", "1", "4", "1"]
bucket1 = []
bucket2 = []
bucket3 = []
bucket4 = []
while "1" in keystring:
if "1" in keystring:
bucket1.append(keystring[keystring.index("1") 1])
print (bucket1)
- Or you can change "numbers" in your
while
loop toint
keystring = [1, 4, 2, 3, 2, 1, 4, 1]
bucket1 = []
bucket2 = []
bucket3 = []
bucket4 = []
while 1 in keystring:
if 1 in keystring:
bucket1.append(keystring[keystring.index(1) 1])
print (bucket1)
I don't know "correct" way to do it because I don' know context of your app but for
loop might be better for this operation. And you are checking same thing in if
statement as while
loop. In while
loop you are searching for 1 and then with if
statement you are checking for 1 again which you already know it's there.
TLDR: You don't need if
statement.