01110100
01110100

Reputation: 833

Python 'for' loop issue, wht are these two variables not adding together properly in my 'for' loop?

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: exdict = {a:1 , b:2 , c:3 , d:4}, I append those to a list as such: exlist = [a, b, c, d, 1, 2, 3, 4]. 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.

I have done research online and on stackoverflow but cannot find any answer that I can apply to my situation...

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 ### ITERATIONS & SETUP 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.

Bug check 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 & 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

edit - My questions is ultimately, why is a + x 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?

Upvotes: 0

Views: 267

Answers (2)

David Robinson
David Robinson

Reputation: 78600

The reason this isn't working as you expect is that your variable x originally means the length of the list, but it's replaced in your for x in range loop- and then you expect it to be equal to the length of the list. You could just change the line to

for i in range(iteration)

instead.

Also note that you could replace all the code in the for loop with

variableletter, variablevalue = random.choice(newdict.items())

Upvotes: 3

corn3lius
corn3lius

Reputation: 4985

Your problem is scope

which x are you looking for here

x = len(z)/2 # This is the first x
print 'x is value %d' % (x)


### ITERATIONS & SETUP

iteration = ri(2,6)

# x in the for loop is referencing the x in range...
for x in range(1,iteration):
    a = ri(1,x)
    m = a + x

Upvotes: 0

Related Questions