Okay, so what I want to do, is to take the following code, and make it so that way it keeps continuning for all of the word.

keycode = raw_input()
matrix = [0, 0, 0, 0]
for x in range(0, len(keycode)):
    y = ord(keycode[x])
    if x > 3 and x <= 7:
        x = x -4
    if x > 7 and x <= 11:
        x = x -8
    if x > 11 and x <= 15:
        x = x -12
    if x > 15 and x <= 19:
        x = x -16
    matrix[x] = matrix[x] + y

The part I want to continue is the if statements... I'm thinking maybe using the len(keycode) somehow, but I'm a bit confused on where to go from here. Any help would be appreciated.

Recommended Answers

All 2 Replies

What about something like this (making use of integer division):

keycode = raw_input()
matrix = [0, 0, 0, 0]
for x in range(0, len(keycode)):
    y = ord(keycode[x])
    factor = x / 4
    x = x - (4 * factor)
    matrix[x] = matrix[x] + y

Not sure if that's what you were shooting for?

Hey, that'd work. But I just figured it out after I posted. Here's what I used:

for x in range(0, len(keycode)):
	y = ord(keycode[x])
	while x > 3:
		x = x -4
	matrix[x] = matrix[x] + y

Then again, yours is probably more efficient. But I tested a keycode of a length of 1,024 charecters, and it finished in about the same time. Thanks for the suggestion though :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.