Python Forum
Question about for loop not creating an infinite loop.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about for loop not creating an infinite loop.
#1
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.
Reply


Messages In This Thread
Question about for loop not creating an infinite loop. - by FWendeburg - Feb-03-2019, 07:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 296 Yesterday, 07:38 PM
Last Post: FortuneCoins
  for loop not executing Abendrot47 2 206 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 605 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 799 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 488 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 365 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 405 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 439 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 620 Dec-28-2023, 05:14 PM
Last Post: trix
  Need an alternative to brute force optimization loop jmbonni 5 1,179 Dec-07-2023, 12:28 PM
Last Post: RockBlok

Forum Jump:

User Panel Messages

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