Bottom Page

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...
Quote
#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
Quote
#3
add a print statement on line 14:
print('spinner1: {}, spinner2: {}'.format(spinner1, spinner2))
and watch it. The error should become apparent
Quote
#4
You may want to check modulo operator %
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Populating a timetable with subjects brittocj 1 186 May-02-2019, 07:00 AM
Last Post: buran
  loop through list or double loop 3Pinter 4 626 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Populating Array2 from Array1 PappaBear 1 454 Aug-22-2018, 04:30 AM
Last Post: PappaBear
  Populating a list with divisors RedSkeleton007 1 488 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 808 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  populating csv and searching the file mepyyeti 1 721 Apr-26-2018, 03:02 AM
Last Post: woooee
  For looping over a list, editing the list from inside the loop? Krookroo 3 1,429 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 2,795 Aug-30-2017, 12:14 PM
Last Post: metulburr
  Importing file in dataframe and populating missing column name vvv 1 1,323 Feb-26-2017, 02:32 PM
Last Post: sparkz_alot

Forum Jump:


Users browsing this thread: 1 Guest(s)