+ 4
[SOLVED] While loop doesnt stop
I wanna make a program which creates n of families and allows firstnames not to equal to lastnames or firstnames. names = ["tri", "ahmad", "doni", "lulu"] firstnames = ["tri", "ahmad"] lastnames = "tri" while firstnames[0] == lastnames or firstnames[0] in firstnames: firstnames[0] = random.choice(names) print(firstnames) So it will brutally change firstnames[0] to random value from names but firstnames and lastnames However it doesnt stop. How can i solve this problem? https://code.sololearn.com/cfFslXcl5hY7/?ref=app https://code.sololearn.com/c62mH5j5d62U/?ref=app
16 ответов
+ 4
What do you mean by
"firstnames not to equal to lastname or firstnames"
fn != fn - > False
+ 4
firstnames[0] in firstnames[1:]
+ 4
for your other code you can do this:
while firstnames[j] in lastnames or firstnames.count(firstnames[j])>1 :
or better
len(set(firstnames)) < len(firstnames)
+ 3
firstnames[0] is always in firstnames
infinite while loop.
+ 3
the first entry of a list is always in the list
firstnames[0] in firstnames
is always true.
+ 2
Doesnt it will only repeat one time?
+ 2
The firstname[0] will be neither lastname nor what is inside firstname
+ 2
I meant it will be random value from names that isnt equal to firstnames or lastnames, my expected output is ["doni/lulu", "ahmad"]
+ 2
Still no output
+ 2
I know, but firstnames[0] will be changed as what i said then while loop stops. i think there is no problem with my script this far
+ 2
I see, now what should i do
+ 1
use if inside the while loop and use a flag to drive while loop.