+ 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

4th May 2021, 4:09 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
16 ответов
4th May 2021, 6:21 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
+ 4
What do you mean by "firstnames not to equal to lastname or firstnames" fn != fn - > False
4th May 2021, 4:42 AM
Louis
Louis - avatar
+ 4
firstnames[0] in firstnames[1:]
4th May 2021, 6:25 AM
Oma Falk
Oma Falk - avatar
+ 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)
4th May 2021, 6:32 AM
Oma Falk
Oma Falk - avatar
+ 3
firstnames[0] is always in firstnames infinite while loop.
4th May 2021, 5:52 AM
Oma Falk
Oma Falk - avatar
+ 3
the first entry of a list is always in the list firstnames[0] in firstnames is always true.
4th May 2021, 6:05 AM
Oma Falk
Oma Falk - avatar
+ 2
Doesnt it will only repeat one time?
4th May 2021, 4:13 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
+ 2
The firstname[0] will be neither lastname nor what is inside firstname
4th May 2021, 4:17 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
+ 2
I meant it will be random value from names that isnt equal to firstnames or lastnames, my expected output is ["doni/lulu", "ahmad"]
4th May 2021, 4:58 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
+ 2
Still no output
4th May 2021, 5:00 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
+ 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
4th May 2021, 6:04 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
+ 2
I see, now what should i do
4th May 2021, 6:17 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
+ 1
use if inside the while loop and use a flag to drive while loop.
4th May 2021, 8:27 PM
Shubham Singh
Shubham Singh - avatar