1

Im trying to make an encryption program

def intro():
    msg = input("Enter the message you wish to encrypt: ")
    return msg

def shift(msg):
    alpha = ['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']
    rotate = int(input("The level of the encryption?: "))
    text = ""
    for ch in msg:
        if ch == " " or ch == ".":
            pass
        elif msg == "$":
            print("nah")
        else:
            index = alpha.index(ch)
            newindex = index + rotate
            new= alpha[newindex]
            text += new
    return text


def start():
    msg = intro()
    text = shift(msg)
    print("Your encryptions is: " + text)


start()

I can't figure out a way to loop the list without getting an index out of range error. For example, if you put "z" it will shift to an "a". I also need for my program to loop till user inputs to end it. I just started coding in python a few months ago so any help will be appreciated!beginner

tom10
  • 67,082
  • 10
  • 127
  • 137
  • Infinite loops can be created with `while True:` – Michael Butscher Nov 18 '17 at 01:48
  • Thank you all for helping me! My code works great now! I have another question if possible to answer. I know ascii for lowercase and uppercase are different. Is there a way I can shift uppercase letters. like make " Hello" shift by 2 to make"Jgnq". Help is always appreciated! –  Nov 18 '17 at 02:09

3 Answers3

1

All you need to do is add this line

newindex %= len(alpha)

Detailed Change (with context)

index = alpha.index(ch)
newindex = index + rotate
new= alpha[newindex]
text += new

to

index = alpha.index(ch)
newindex = index + rotate
newindex %= len(alpha) # <--- this is the new line
new= alpha[newindex]
text += new

This will automatically make the new index loop so it never goes past the end!

Working example

>> Enter the message you wish to encrypt: 'xyz'
>> The level of the encryption?: 2
>> Your encryptions is: zab
joshuakcockrell
  • 5,200
  • 2
  • 34
  • 47
0

use the modulus operator to wrap the index around when it's outside the list length:

newindex = (index + rotate) % len(alpha)

To repeat, use a while True: loop, and use break to end it.

def start():
    while True:
        msg = intro()
        if msg == '':
            break
        text = shift(msg)
        print("Your encryptions is: " + text)

This will end when the user inputs an empty line.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Since your code is running fine, I can tell you about some techniques you can work on to get the functionality you want.

To get an array that loops around, you can use a mod system. For example 8 mod 3 = 2 and it would be coded remainder = 8 % 3. If you had a mod size 26, i.e. the alphabet, you could take the remainder of the total number and use it as an index in your alphabet list. This would cycle around when the total number is greater than 26 and begin again at a.

To get the program to end on user input, you can use a variety of methods such as keyboard interrupts, recognizing certain commands such as ctrl-c or whole words. Here is a start from a previous stackoverflow question. How to kill a while loop with a keystroke?

pastaleg
  • 1,782
  • 2
  • 17
  • 23