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
#1
Hi folks,

I am an older fella, who has worked in and around IT for many years and who decided to take on a new challenge and learn to code - I haven't done any coding since about 1987 when I did a small amount of C and PASCAL in college when I left the military. My Cyber Security colleagues convinced me that Python was the way to go so i've got an online tutorial underway along with an eBook from NoStarchPress.

Because my brain doesn't retain things as it did in 1987 I need to do and redo some stuff to get it to sink in so I try to adapt some of the exercises and lessons after we complete a section to confirm my understanding. When we were playing with lists and loops I tried to combine the two subjects and I have some unexpected behaviour. I am hoping that I can develop my skills with the help of the forum and later give back to any other newcomers, but for now I am afraid this is brand new to me - Ive been at it for two weekends.

I am using a Mac on High Sierra, Sublime Text as an editor, pre-instaalled Python (2.7.10)

this is my simple code to practice with lists and loops, I create a list, add to the list, remove items from the list and move them to a second list:

 # -*- coding: utf-8 -*-
# start with a newlist
guests = ['David', 'Adolf','Charles']
print(guests)

# add some more entries to the list, add to first, second and last
guests.insert(0, 'Geoff')
guests.insert(1, 'Colin')
guests.append('Fred')

print(guests) # print the list to check its valid
print('\n')

# start to remove guests and add them to an alternate list
removedlist = []

print('there are ' + str(len(guests)) + ' items in the list \n')
for guest in guests:
	popped=guests.pop()
	removedlist.append(popped)
	print('last popped value - ' + popped)
	print('current guests list')
	print(guests)
	print('the following have been removed')
	print(removedlist)
	print('\n')
I expected the for loop to iterate six times and for all the entries in "guests" to end up in "removedlist" but it only appears to iterate three times:

This is the output:

Output:
['David', 'Adolf', 'Charles'] ['Geoff', 'Colin', 'David', 'Adolf', 'Charles', 'Fred'] there are 6 items in the list last popped value - Fred current guests list ['Geoff', 'Colin', 'David', 'Adolf', 'Charles'] the following have been removed ['Fred'] last popped value - Charles current guests list ['Geoff', 'Colin', 'David', 'Adolf'] the following have been removed ['Fred', 'Charles'] last popped value - Adolf current guests list ['Geoff', 'Colin', 'David'] the following have been removed ['Fred', 'Charles', 'Adolf'] [Finished in 0.1s]
All help and guidance is appreciated on both this query and the wider "how to learn python" question
Reply


Messages In This Thread
Old brain - New Language - Question on loops - by sailingbikeruk - Sep-30-2018, 03:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to fix my brain on classes blackknite 2 1,325 May-01-2022, 12:25 PM
Last Post: ibreeden
  Back to loops question wernerwendy 1 2,960 Jun-17-2017, 10:02 PM
Last Post: ichabod801
  Question on runtime and improving nested for loops ackmondual 1 3,144 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