Python Forum
problem about 'if' and 'for' from a python beginner
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem about 'if' and 'for' from a python beginner
#1
The python code is as below:
curren_accounts=['amy','ADA','eric','tim','jasmin']
new_accounts=['torge','tom','ada','ace','amanda']
for new_account in new_accounts:
	for curren_account in curren_accounts:
		if new_account.title() == curren_account.title():
			print('"'+new_account+'"'+' is already used, please find a new name.')
			new_accounts.remove(new_account)
			print(new_accounts)
	if new_account in new_accounts:
		print('"'+new_account+'"'' is not be used yet.')
the result is as below:
"torge" is not be used yet.
"tom" is not be used yet.
"ada" is already used, please find a new name.
['torge', 'tom', 'ace', 'amanda']
"amanda" is not be used yet.

So the question is why 'ace' in the new_account is not processed in the result.
Many thanks!
Reply
#2
Because you change new_accounts list while iterating over it by removing ada from it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I'm a little surprised this sort of thing isn't in the tutorials sub-forum somewhere that can be easily linked to, like we do with the or keyword.
Reply
#4
Have a look at sets. This could be done in couple of lines using sets
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  small beginner problem FelixReiter 2 1,816 Nov-17-2020, 03:26 PM
Last Post: FelixReiter
  Beginner having Syntax Error problem RyanHo 3 2,332 Sep-10-2020, 08:33 AM
Last Post: cnull
  Beginner, my recursion returns None, and related problem zpacemanzpiff 2 1,741 Jul-02-2020, 04:25 AM
Last Post: zpacemanzpiff
  Beginner problem, replace function with for loop Motley_Cow 9 4,535 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  Beginner problem in python script Cedmo 3 2,722 Jul-04-2019, 08:22 PM
Last Post: Cedmo
  Beginner Problem python 2.7 Jonathan_levy 2 2,635 Jul-04-2018, 08:46 PM
Last Post: ichabod801
  Beginner Problem python 2.7 Jonathan_levy 2 2,680 Jul-03-2018, 11:58 AM
Last Post: gruntfutuk
  Beginner. Calculator problem ¯\_(ツ)_/¯ stykus1992 0 2,292 Feb-15-2018, 11:01 AM
Last Post: stykus1992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020