Caesar cypher in Python

2024/7/5 11:14:03

I am new to python and I want to write a program that asks for a line of input (each number will be separated by a single space.) It would use a simple letter-number substitution cipher. Each letter will be appointed a number. So 1 = a, 2 = b and 3 = c until it reaches 26 = z. From there, however, the cipher would continue on so; 27 = a, 28 = b etc. 0's will be a space. The program will only use 0's and positive numbers. It would also print out the decryption of the message. For example:

Please type a code: 8 5 12 12 15

hello

and another example:

Please type a code: 16 25 20 8 14 0 9 19 0 3 15 15 12

python is cool

At the moment i have tried turning the alphabet into a list like this;

    n = int(input("Please type a code: ")x =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

...and then referring back to it later. However, im not exactly sure how this would work. I've also tried using the .replace() function. Like this:

    n = int(input("Please type a code: ")n = n.replace('1','a') #all the way until z and then loop it.print(n)

and so and and so on. But again, i have no idea how to do this properly. Any help would be greatly appreciated. Thanks.

Answer

Use split()

numbers = input("Please type a code: ").split()# ['16', '25', '20', '8', '14', '0', '9', ',19', '0', '3', '15', '15', '12']

Use for .. in ..

for num in numbers:print( x[int(num)] )

If you use 0 as space you have to add space at the begginig of list

x = [' ', 'a', 'b', 'c', .... ]

now x[0] == ' '

https://en.xdnf.cn/q/120384.html

Related Q&A

Flatten a list with sublists and strings [duplicate]

This question already has answers here:Flatten an irregular (arbitrarily nested) list of lists(54 answers)How do I make a flat list out of a list of lists?(32 answers)Closed 2 years ago.How can I flat…

Guitar string code in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

What is the point of initializing extensions with the flask instance in Flask? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Nested list doesnt work properly

import re def get_number(element):re_number = re.match("(\d+\.?\d*)", element)if re_number:return float(re_number.group(1))else:return 1.0def getvalues(equation):elements = re.findall("…

How can I increment array with loop?

Ive got two lists:listOne = [33.325556, 59.8149016457, 51.1289412359] listTwo = [2.5929778, 1.57945488999, 8.57262235411]I use len to tell me how many items are in my list:itemsInListOne = int(len(list…

Two Complements Python (with as least bits as possible)

I am trying to output the binary representation of an negative number with the least bytes available each time.Example:-3 -> 101 -10 -> 10110

iterating through a column in dataframe and creating a list with name of the column + str

I have a dataframe with 2 coulmn, i want to iterate through the column headers, use that value and add a string to it and use it as the name of a list.rrresampled=pd.DataFrame() resampled[RAT]=dd1[RAT]…

Python multiple inheritance name clashes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

How to read multiple txt file from a single folder in Python? [duplicate]

This question already has answers here:How to open every file in a folder(8 answers)Closed 3 years ago.How can I read multiple txt file from a single folder in Python?I tried with the following code b…

Time Changing Without Clicking Button

The following code works fine as long as the time in the spinbox does not change. What I want is to do the set the time for a break. The first time it works perfectly but after that if I change the val…