Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
repeating
#1
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 Tongue

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:
Output:
enter something: heey ['heey', '2', '3']
buran write Oct-24-2020, 11:38 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How would I go about repeating/looping this code? azulu 5 2,125 Mar-23-2020, 02:26 AM
Last Post: azulu

Forum Jump:

User Panel Messages

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