## Assistance required: Beginner Python Programming

This is the place for queries that don't fit in any of the other categories.

### Assistance required: Beginner Python Programming

i am writing a encrypting program which process is as follows
- Spaces in a message replaced by X's
-Each word in the original message is reversed
- Consecutive sequences of words, called “blocks”, have the order of the words
reversed. The size of these blocks is a parameter to the encryption function and acts
as the “key”. For example, if the message was 'THE PRICE OF FREEDOM IS
ETERNAL VIGILENCE' and the block size is four then each block of four words
will be reversed, producing 'FREEDOM OF PRICE THE VIGILENCE ETERNAL
IS' (ignoring the other two steps above). Notice that the last block only has three
words in this case, but it is still reversed nonetheless.

now i've been able to replace the spaces and reverse everything, but the last step has stumped me.

the following code is what i have so far.

Code: Select all
`def encrypt (words, block):    words = words [::-1]    midpoint = len(words)/block    first_half = words[0:midpoint]    second_half = words[midpoint:]    words = first_half + second_half    words = words.replace(' ', 'X')    return wordsdef decrypt (wordsde, block):    wordsde = wordsde[::-1]    midpoint = len(wordsde) / block    first_half = wordsde[:midpoint]    second_half = wordsde[midpoint:]    wordsde = first_half + second_half    wordsde = wordsde.replace('X', ' ')    wordsde = wordsde.strip()    return wordsde`

but everytime it can't read the block or something:

Code: Select all
`File "__main__", line 18, in __main__Failed example:    encrypt('WHO WATCHES THE WATCHERS', 2) # Test 4Expected:    'SEHCTAWXOHWXSREHCTAWXEHT'Got:    'SREHCTAWXEHTXSEHCTAWXOHW'Trying:    encrypt('PARANOIA IS OUR PROFESSION', 3) # Test 5Expecting:    'RUOXSIXAIONARAPXNOISSEFORP'**********************************************************************File "__main__", line 22, in __main__Failed example:    encrypt('PARANOIA IS OUR PROFESSION', 3) # Test 5Expected:    'RUOXSIXAIONARAPXNOISSEFORP'Got:    'NOISSEFORPXRUOXSIXAIONARAP'Trying:    encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4) # Test 6Expecting:    'MODEERFXFOXECIRPXEHTXECNELIGIVXLANRETEXSI'**********************************************************************File "__main__", line 26, in __main__Failed example:    encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4) # Test 6Expected:    'MODEERFXFOXECIRPXEHTXECNELIGIVXLANRETEXSI'Got:    'ECNELIGIVXLANRETEXSIXMODEERFXFOXECIRPXEHT'`

Rippa18

Posts: 1
Joined: Sun Mar 31, 2013 10:09 am

### Re: Assistance required: Beginner Python Programming

First split the sentence into separate words in a list using split.
Then iterate through the list with a for loop; reverse each word; and insert it in a new list appropriately.
The insertion index point will be something like:
Code: Select all
`(i//size)*size`
Then just turn the list back into a string using join.
(that last step can also be used to add the "X"s for spaces.)

I'm trying to avoid just giving you a solution so give this a try first.

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1504
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona