repeating - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: repeating (/thread-30516.html) |
repeating - pastakipp - Oct-24-2020 hello guys, when i run this code i can only change 1 item in the list, but i want to repeat the code untill all the three items in the list are changed to items bigger then len() 3, but i cant figure out how to do that. is it even possible? like, i want to get something like this: ['heey', 'whatsupp', 'ineadtolearnthis'] so actually my question is: can i change every string in my list step by step with using a while loop? everytime i press enter that it will change one string and then press enter again i can input another string untill every string is longer then len()3? i want to say sorry for my english as it is not my first language, i want to thank you guys for reading import random list = ['1', '2', '3'] x = random.choice(list) for index, character in enumerate(list): if character == x: if len(character) < 3: list[index] = input('enter something: ') while len(list) < 3: list[index] = input('enter something: ') print(list)result:
RE: repeating - jefsummers - Oct-24-2020 It's a little unclear what you are wanting to do, but I assume list and string manipulation. Because in line 8 len(list) is equal to 3, line 9 is never executed. It is a very bad idea to name a variable the same as a keyword. It overrides the keyword and leads to behavior that is more interesting than you want. In this case, you should rename list to list_of_items or similar. |