Python Forum
Old brain - New Language - Question on loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Old brain - New Language - Question on loops
#2
Quote:Because my brain doesn't retain things as it did in 1987
You're too young for this! Need to go on Gluten Free Diet. I will vouch for it, 70+ YO.

Quote:and the wider "how to learn python" question
Trying things like you're doing now is good.
I recommend this tutorial (my favorite): https://www.python-course.eu/python3_course.php

i also recommend installing VSCode (Not Visual Studio, unrelated to MS) with it you can run the debugger, and
watch the guests list expand and contract as you execute step by step.
Output:
guests = ['David', 'Adolf','Charles'] Guests: ['David', 'Adolf','Charles'] guests.insert(0, 'Geoff') Guests: ['Geoff', 'David', 'Adolf','Charles'] guests.insert(1, 'Colin') Guests: ['Geoff', 'Colin', 'David', 'Adolf', 'Charles'] guests.append('Fred') Guests: ['Geoff', 'Colin', 'David', 'Adolf', 'Charles', 'Fred'] there are 6 items in the list Iteration 1: for guest in guests: # Here's the problem, see below popped=guests.pop() ['Geoff', 'Colin', 'David', 'Adolf', 'Charles'] last popped value - Fred
The problem is that you are using the contents of the list to control the iterations.
The contents is a moving target so soesn't perform as you expect.
If you want to xcontinue until the list is empty, instead of loop, use:
while guests:
    popped=guests.pop()
    removedlist.append(popped)
    ...
Reply


Messages In This Thread
RE: Old brain - New Language - Question on loops - by Larz60+ - Sep-30-2018, 04:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to fix my brain on classes blackknite 2 1,274 May-01-2022, 12:25 PM
Last Post: ibreeden
  Back to loops question wernerwendy 1 2,935 Jun-17-2017, 10:02 PM
Last Post: ichabod801
  Question on runtime and improving nested for loops ackmondual 1 3,101 Jun-13-2017, 11:11 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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