-2

I was supposed to create strings in my python function to show that the encryption and decryption of my code was working properly. Below is my code.

def encrypt1():
    plain1 = input('Enter plain text message: ')
    cipher = ''

    for each in plain1:
        c = (ord(each)+3) % 126

        if c < 32: 
            c+=31

    cipher += chr(c)

    print ("Your encrypted message is:" + cipher)

encrypt1()

1.) I get the "Inconsistent use of tabs and spaces" error

2.) How should I input set, constant strings to get the wanted input check if my code works (for example, type in meet and get the correct input, etc)

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • you just have to indent this line `cipher += chr(c)`. Also, passing argument `plain1` to the function doesn't make sense since you overwrite it using `input()`. The python versions have nothing to do with this – Ma0 Sep 14 '16 at 14:44
  • ...do you have a question? – jonrsharpe Sep 14 '16 at 14:45

1 Answers1

1

Your code is mostly fine but there are a few issues that I've pointed out:

  1. Your indenting is off. The line cipher += chr(c) should be indented to match the things in the for loop
  2. The function encypt1() shouldn't take a parameter; you are setting plain1 in the method itself so you can declare it there
  3. If you want to deal with just lowercase letters, you should be doing % 123 (value of 'z') and the if clause should check if c < 97. If you want to wrap around printable ascii what you're doing is fine.

This gives:

    def encrypt1():
        plain1 = input('Enter plain text message: ')
        cipher = ''
        for each in plain1:
            c = (ord(each)+3) % 126
            if c < 32: 
                c+=31
            cipher += chr(c)
        print ("Your encrypted message is:" + cipher)

Because you want to be able to test this on multiple strings, you would want to pass plain1 as a parameter to the function. But you also want the user to be able to input plain1 if a parameter isn't passed in to the function. For that I would suggest a default parameter. Look at the commented code below:

def encrypt1(plain1=""):        # if no argument is passed in, plain1 will be ""
    if not plain1:              # check if plain1 == "" and if so, read input from user
        plain1 = input('Enter plain text message: ')
    cipher = ''
    for each in plain1:
        c = (ord(each)+3) % 126
        if c < 32: 
            c+=31
        cipher += chr(c)
    return cipher               # rather than printing the string, return it instead

So to test this you could do:

test_strings = ['hello world', 'harambe', 'Name', 'InputString']

for phrase in test_strings:
    print ("Your encrypted message is:" + encrypt1(phrase))

Which gives the output:

Your encrypted message is:khoor#zruog
Your encrypted message is:kdudpeh
Your encrypted message is:Qdph
Your encrypted message is:LqsxwVwulqj
gowrath
  • 3,136
  • 2
  • 17
  • 32
  • Your answer solves my error problems, but I still don't know how to input the strings :p – Yeo Zhe Yong Sep 14 '16 at 15:02
  • @YeoZheYong Ah I just saw the edit to your question. What do you mean input the strings? Do you want a way to test this on multiple strings somehow? – gowrath Sep 14 '16 at 15:03
  • yes, sorry if I'm making a fool of myself, but our university module mostly involves html and javascript, with only a little bit of python, so inexperienced people like me end up rather confused when python/php questions come out – Yeo Zhe Yong Sep 14 '16 at 15:07
  • @YeoZheYong No worries, I've updated my answer. Let me know if I understood your question correctly and feel free to ask for clarification. – gowrath Sep 14 '16 at 15:11
  • 1
    you can also use `plain1 = plain1 or input('Enter plain text message: ')` since `or` shortcircuits. Also +1 for test string 'harambe' – Ma0 Sep 14 '16 at 15:13