muutujad = list(input("Muutujad (sisesta formaadis A,B,C,...): "))
while "," in muutujad == True:
muutujad.remove(",")
print (muutujad)
My brain says that this code should remove all the commas from the list and in the end
the list should contain only [“A”,”B”,”C” ….] but it still contains all the elements. When i tried to visualize the code online, it said like [ “,” in muutujad ] is always False but when i check the same command from the console it says it is True. I know it is a simple question but i would like to understand the basics.
You can use a list comprehension instead of a while loop:
Your
iftest itself is also wrong. You never need to test for== Trueforifin any case, that’s whatifdoes. But in your case you test the following:which is always going to be False. In python, comparison operators like
inand==are chained. Leaving off the== Truewould make yourwhileloop work much better.I’m not sure you understand what happens when you call
list()on a string though; it’ll split it into individual characters:If you wanted to split the input into elements separated by a comma, use the
.split()method instead, and you won’t have to remove the commas at all: