[SOLVED] While loop doesnt stop | Sololearn: Learn to code for FREE!

+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

5/4/2021 4:09:11 AM

Tri Satria [NEW]

16 Answers

New Answer

+3

Solved https://code.sololearn.com/cA24a21a23a1/?ref=app

+4

Just change the "while" statement to "if"

+4

Have you tried to run this code on your local IDE and what's the output. I think there's nothing to correct in your code. Not getting an output is probably a sololearn issue cos i edited your code and added some print statement at the top level and there's no output https://code.sololearn.com/czm8QDpC5FTB/?ref=app

+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

Are you trying to create a unique name so that ! (firstNames[0] == lastName)

+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.