Note that there are some explanatory texts on larger screens.

plurals
  1. POPython 'for' loop issue, wht are these two variables not adding together properly in my 'for' loop?
    primarykey
    data
    text
    <p>I am writing a code snippet for a random algebraic equation generator for a larger project. Up to this point, everything has worked well. The main issue is simple. I combined the contents of a dictionary in sequential order. So for sake of argument, say the dictionary is: <code>exdict = {a:1 , b:2 , c:3 , d:4}</code>, I append those to a list as such: <code>exlist = [a, b, c, d, 1, 2, 3, 4]</code>. The length of my list is 8, which half of that is obviously 4. The algorithm is quite simple, whatever random number is generated between 1-4(or as python knows as 0-3 index), if you add half of the length of the list to that index value, you will have the correct value. </p> <p>I have done research online and on stackoverflow but cannot find any answer that I can apply to my situation...</p> <p>Below is the bug check version of my code. It prints out each variable as it happens. The issue I am having is towards the bottom, under the <code>### ITERATIONS &amp; SETUP</code> comment. The rest of the code is there so it can be ran properly. The primary issue is that a + x should be m, but a + x never equals m, m is always tragically lower.</p> <p>Bug check code:</p> <pre><code>from random import randint as ri from random import shuffle as sh #def randomassortment(): letterss = ['a','b','x','d','x','f','u','h','i','x','k','l','m','z','y','x'] rndmletters = letterss[ri(1,15)] global newdict newdict = {} numberss = [] for x in range(1,20): #range defines max number in equation numberss.append(ri(1,20)) for x in range(1,20): rndmnumber = numberss[ri(1,18)] rndmletters = letterss[ri(1,15)] newdict[rndmletters] = rndmnumber #x = randomassortment() #print x[] z = [] # set variable letter : values in list for a in newdict.keys(): z.append(a) for b in newdict.values(): z.append(b) x = len(z)/2 test = len(z) print 'x is value %d' % (x) ### ITERATIONS &amp; SETUP iteration = ri(2,6) for x in range(1,iteration): a = ri(1,x) m = a + x print 'a is value: %d' % (a) print 'm is value %d' %(m) print variableletter = z[a] variablevalue = z[m] # variableletter , variablevalue </code></pre> <p>edit - My questions is ultimately, why is <code>a + x</code> returning a value that isn't a + x. If you run this code, it will print x , a , and m. m is supposed to be the value of a + x, but for some reason, it isnt?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload