Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:10:45+00:00 2026-05-30T14:10:45+00:00

I’m a beginner when it comes to programming and Python. I’ve done most of

  • 0

I’m a beginner when it comes to programming and Python. I’ve done most of Learn Python the Hard Way and I’m now working through Invent Your Own Computer Games with Python.

There is a chapter in the “Invent” book where you create a simple cipher program that encrypts a user submitted phrase using a key number to offset ASCII codes and using that key number to decrpyt as well. Later in the chapter, a “brute force” decryption method is added which runs through all 26 possible translations of the message and prints each of them.

I wanted to go one step further and print only the correctly translated message rather than having to look through all 26. I created a string with common words in English and tried various methods to test each generated output against that string. So far, I have been unable to find any method that works.

Here is the decryption code: (the indent blocks are correct in IDLE but not here.)

def getTranslatedMessage(mode, message, key):
if mode[0] == 'd':
    key = -key
translated = ''

for symbol in message:
    if symbol.isalpha():
        num = ord(symbol)
        num += key

        if symbol.isupper():
            if num > ord('Z'): num -= 26
            elif num < ord('A'): num += 26
        elif symbol.islower():
            if num > ord('z'): num -= 26
            elif num < ord('a'): num += 26

        translated += chr(num)
    else: translated += symbol
return translated

so when the user selects “brute force” mode, and types in a message, the following function is called:

def brute():
for key in range(1, MAX_KEY_SIZE + 1):

    if words in getTranslatedMessage('decrypt', message, key).split():
        print(getTranslatedMessage('decrypt', message, key))

and finally, here is the list of words I’m checking against:

words = 'and is the but or not may in it why how who where'.split()

edit:
Here is what I have now. It doesn’t work…

def brute():
    for key in range(1, MAX_KEY_SIZE + 1):
        if set(getTranslatedMessage('decrypt', message, key)).intersection(words_set):
            print(getTranslatedMessage('decrypt', message, key))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:10:47+00:00Added an answer on May 30, 2026 at 2:10 pm

    The problem comes down to this line:

    if words in getTranslatedMessage('decrypt', message, key).split():
    

    You can’t compare lists like that:

    >>> a = [1,2,3]
    >>> b = [2,3,4]
    >>> if a in b:
    ...     print ("yep!")
    ... 
    >>> 
    

    if words in somelist checks to see whether words is an element of somelist. For example:

    >>> a in b
    False
    >>> a in [[1,2,3],4,5]
    True
    

    You have some other alternatives, though.

    >>> if set(a).intersection(b):
    ...     print("yep!")
    ... 
    yep!
    >>> if any(i in b for i in a):
    ...     print("also yep, but slow")
    ... 
    also yep, but slow
    >>> bset = set(b)
    >>> if any(i in bset for i in a):
    ...     print("still yep, not so bad")
    ... 
    still yep, not so bad
    

    and so on, with various levels of efficiency. Sets have very fast membership tests, so it’s generally better to use those if performance is an issue. The first any could take len(a) * len(b) steps. After fixing this (and changing brute to accept a message argument), I find your code gives:

    >>> m = getTranslatedMessage("d", "this may work", 17)
    >>> print(m)
    cqrb vjh fxat
    >>> brute(m)
    this may work
    

    which I think is what you wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.