I have a strange problem which I cant figure out for some time now. I have 4 nested for loops and in the last one:
JavaScript
x
17
17
1
for a in range(1, 10):
2
for b in range(1, 10):
3
for c in range(1,10):
4
for d in range(1, 10):
5
if flag_c_d and flag_a_b and control_flag:
6
print(f'{a}{b}{c}{d}',end=' ')
7
counter += 1
8
if counter == 4:
9
password = True
10
passcode += a*1000 +b*100 +c*10 + d
11
if counter == 0 or counter <4:
12
password = False
13
if password:
14
print(f'Password: {passcode}')
15
else:
16
print(f'No!')
17
How can I remove the blank space the last time it prints, i.e the last time all flags are True
?
And I have to note that I still haven’t learned functions and lists.
Advertisement
Answer
Ok, this is a possible solution:
JavaScript
1
6
1
separator=''
2
for .
3
if flag_1 and flag_2 and flag_3 :
4
print(f'{separator}{a}{b}{c}{d}',end='')
5
separator=' '
6
You can also consider using functools.reduce