Python programming challenges-using Python to implement Engma--chapter 1

Source: Internet
Author: User

Think of a fun thing to do, using Python to implement the German device that was encrypted during World War II-Engma.

So what is Engma, and how does he work? This article provides a very detailed description:

https://www.zhihu.com/question/28397034

Please look at the high-ticket answer.

Road step by step, rice stutter, below, we also come step by step to achieve Engma:

Chapter 1

Use Python to implement simple letter substitution:

When I enter a string of strings (the string is not punctuation, only letters and spaces), the program will follow the pre-set letter substitution table, simply replace the password.

Such as:

Abcdefghjiklmnopqrstuvwxyz

Qwertyuiopasdfghjklzxcvbnm

So when I enter Hello World, it should automatically replace me with ITSSG Vgkar

Try it!

The code is as follows: Using the Maketrans method of string, do you have any idea?

#-*-Coding:utf-8-*-# The purpose of this code is to implement the German World War II cipher machine via Python: Engma # First, the first step is to define a simple function: simple character substitution import Reimport stringdef simple_ Replace (password, replace_word):    If not type (password) = = Type (replace_word) = = Type (' a '):        print (' Password must be a string! ')        return False an    = Re.match (' [a-z\s]+$ ', password)    if not a:        print (' string can only contain lowercase letters and spaces!) ')        return False    If Len (replace_word)! =:        print (' The replacement code must be 26 letters!) ')        return False    ori_table = ' abcdefghijklmnopqrstuvwxyz '    table = Str.maketrans (ori_table, Replace_word )    return str.translate (password, table) A_password = input (' Please enter clear text: ') R_password = ' qwertyuiopasdfghjklzxcvbnm ' Print (' Ciphertext as follows: ', Simple_replace (A_password, R_password))

Python programming challenges-using Python to implement Engma--chapter 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.