Python Forum
for loop and list populating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for loop and list populating
#1
Ok I built a very basic caesar wheel...(w/out modules/classes). There was more trial/error than I care to admit Tongue . But, I came across something I couldn't figure out. Here is the relevant snippet.

base_wheel = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z']

new_wheel1 = []
new_wheel2 = []

spinner1 = random.randint(0,25)
spinner2 = spinner1

for b in base_wheel:
	new_wheel1.append(base_wheel[spinner1])
	spinner1 -= 1#no problem with decrementing
for b in base_wheel:
	new_wheel2.append(base_wheel[spinner2])

	#why does += 1 (below) go over range?  I am looping elements of base_wheel
	#loop is finite so spinner2 must also terminate
	spinner2 +=1
very puzzling to me... thx in advance...
Reply
#2
What is unclear? You have some initial value and you add 26 to it. It will always be greater than 25 (max index in wheel). You don't have problem with decreasing, because python also supports negative indexes. I.e. base_wheel[-1] is z
I.e any value between -25:25 is valid index
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
add a print statement on line 14:
print('spinner1: {}, spinner2: {}'.format(spinner1, spinner2))
and watch it. The error should become apparent
Reply
#4
You may want to check modulo operator %
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Populating an array dynamically zxcv101 1 1,106 May-17-2022, 11:24 AM
Last Post: deanhystad
  Appending to list of list in For loop nico_mnbl 2 2,319 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,308 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Populating a timetable with subjects brittocj 1 1,776 May-02-2019, 07:00 AM
Last Post: buran
  loop through list or double loop 3Pinter 4 3,386 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Populating Array2 from Array1 PappaBear 1 2,033 Aug-22-2018, 04:30 AM
Last Post: PappaBear
  Populating a list with divisors RedSkeleton007 1 2,147 Aug-21-2018, 12:52 AM
Last Post: Larz60+
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 3,716 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  populating csv and searching the file mepyyeti 1 2,923 Apr-26-2018, 03:02 AM
Last Post: woooee
  For looping over a list, editing the list from inside the loop? Krookroo 3 3,893 Sep-04-2017, 05:08 PM
Last Post: Krookroo

Forum Jump:

User Panel Messages

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