I am trying to make a program that will generate a list filled with randomly generated characters, then repeatedly randomizing another until both of them match up.
However, when I run the following program, even at the lowest value, it will continuously run, forever.
JavaScript
x
25
25
1
import random
2
import string
3
4
combo_length = int(input("pick the number of characters(please write in numbers ONLY): "))
5
combo_characters = string.ascii_letters + string.digits + string.punctuation
6
combo = []
7
for x in range(combo_length):
8
combo.append(random.choice(combo_characters))
9
print(''.join(combo))
10
11
combo2 = []
12
for x in range(combo_length):
13
combo2.append(random.choice(combo_characters))
14
print(''.join(combo2))
15
16
count = 0
17
while combo != combo2:
18
count=count+1
19
for x in range(combo_length):
20
combo2.append(random.choice(combo_characters))
21
print("testing")
22
print(''.join(combo2))
23
print("the word was", combo)
24
print("it took", count, "attempts")
25
Advertisement
Answer
You keep on appending to your second list in your while loop. Simple reset your list during each iteration:
JavaScript
1
23
23
1
import random
2
import string
3
combo_length = 3
4
combo_characters = string.ascii_letters + string.digits + string.punctuation
5
combo = []
6
for x in range(combo_length):
7
combo.append(random.choice(combo_characters))
8
print(''.join(combo))
9
10
combo2 = []
11
for x in range(combo_length):
12
combo2.append(random.choice(combo_characters))
13
print(''.join(combo2))
14
15
count = 0
16
while combo != combo2:
17
count=count+1
18
combo2 = [] # <-- reset list
19
for x in range(combo_length):
20
combo2.append(random.choice(combo_characters))
21
print("the word was", ''.join(combo2))
22
print("it took", count, "attempts")
23