Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Question about for loop not creating an infinite loop. FWendeburg Programmer named Tim Posts: 14 Threads: 10 Joined: Jan 2019 Reputation: 0 Likes received: 0 #1 Feb-03-2019, 07:26 PM Hi, I'm new to programming with python and I completed one of the exercises of the book I use to learn but I don't understand why this code doesn't make an infinite loop of printing the list. The problem is: Write a function called make_great() that modifies the list of magicians by adding the phrase the Great to each magician’s name. Matthes, Eric. Python Crash Course: A Hands-On, Project-Based Introduction to Programming (p. 150). No Starch Press. Kindle Edition. ```magicians = ['xerath', 'ryze', 'sylas', 'vladimir'] def make_great(list_of_magicians): for magician in list_of_magicians: person = list_of_magicians.pop() add_text = 'the great ' + person list_of_magicians.insert(0, add_text) print(list_of_magicians) make_great(magicians) ```This is the output: ``````Output: ['the great vladimir', 'xerath', 'ryze', 'sylas'] ['the great sylas', 'the great vladimir', 'xerath', 'ryze'] ['the great ryze', 'the great sylas', 'the great vladimir', 'xerath'] ['the great xerath', 'the great ryze', 'the great sylas', 'the great vladimir'] ``````I have tried another solution to this problem by doing this: ```magicians = ['xerath', 'ryze', 'sylas', 'vladimir'] def make_great(list_of_magicians): for magician in list_of_magicians: add_text = 'the great ' + magician list_of_magicians.insert(0, add_text) print(list_of_magicians) make_great(magicians) ```And this is the output: ``````Output: ['the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] ['the great xerath', 'the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] ['the great xerath', 'the great xerath', 'the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] ['the great xerath', 'the great xerath', 'the great xerath', 'the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] ['the great xerath', 'the great xerath', 'the great xerath', 'the great xerath', 'the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] -This continues by adding one more element to the list each time.- ``````Can someone explain to me why this two solutions make different outputs? Thanks ! :D. ichabod801 Bunny Rabbit Posts: 3,521 Threads: 73 Joined: Sep 2016 Reputation: 197 Likes received: 1072 #2 Feb-03-2019, 08:45 PM The pop method removes the last item from the list, as well as returning that item. So the first loop shrinks the list by one before growing it by one, thus keeping it the same size. The second one has no pop, so items are not removed (iterating with a for loop does not remove items from the list). Note that modifying a list while you are iterating over it is a bad idea. It works out in the code above, but it doesn't always. A better way to do what your exercise wants is to build a new list: ```great_magicians = [] for magician in magicians: the_great = 'the great {}'.format(magician) great_magicians.append(the_great) print(great_magicians) ```The format method of strings is recommended over string addition for efficiency reasons. In 3.6 or later, look into f-strings. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Breaking subprocess loop from parent process kapibara 2 87 Aug-19-2019, 10:17 AM Last Post: Gribouillis Operations on indexed variables in loop Fibulavie 1 68 Aug-14-2019, 06:07 AM Last Post: fishhook how do i loop a "def ..." when error occurs? Stan2292 0 58 Aug-12-2019, 05:28 AM Last Post: Stan2292 Get all values of for loop with an index BollerwagenIng 2 144 Aug-09-2019, 07:58 AM Last Post: BollerwagenIng Setting maximum value of Range() in For loop pmt 4 209 Aug-04-2019, 02:38 PM Last Post: pmt While loop kasper1903 4 211 Aug-03-2019, 06:27 PM Last Post: jefsummers How can I sort my names of files in for loop? Mike Ru 2 128 Aug-02-2019, 04:56 AM Last Post: perfringo Loop through List jeran042 5 145 Jul-30-2019, 05:25 PM Last Post: ichabod801 Loop do not start for letter matching algoritm phob0s 3 123 Jul-29-2019, 04:31 PM Last Post: ThomasL Why does modifying a list in a for loop not seem to work? umut3806 2 170 Jul-22-2019, 08:25 PM Last Post: umut3806

Forum Jump:

Users browsing this thread: 1 Guest(s)