Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Caesar Cipher Help
#1
Hello all,

I would like some expert python help. I'm very new to Python and I'm trying to create a simple caesar but it isn't working. It's stuck on the selection screen and it won't go any further.


def decrypt():
    a=raw_input("Give me the word to decrypt:")
    number=input("What was it shifted by?")
    b=list(a)
    str(b)
    c=[ord(x)for x in(b)]
    d=[]
    for i in c:
        d.append(i-number)
    e=[chr(i) for i in (d)]
    e="".join(e)
    print ("Decryption Successful, your word is ",e,"!")

def encrypt():
    a=raw_input("Give me a word:")
    number=input("Give me a number:")
    b=list(a)
    str(b)
    c=[ord(x)for x in(b)]
    d=[]
    for i in c:
        d.append(i+number)
    e=[chr(i) for i in (d)]
    e="".join(e)
    print ("Your Caesar shifted result (ascii code) is",e,"!")
    print ("Your key is ",number," remember that!")

def menu():    
    print ("\n\n\nWelcome to the Caesar Shifter.")
    print ("What would you like to do?")
    print ("Option 1:Encrypt Word")
    print ("Option 2:Decrypt Word")
    print ("If you would like to quit, press 0.")
    choice=input("Pick your selection: ")
    if choice==1:
        run=encrypt()
        run
        menu()
    elif choice==2:
        derun=decrypt()
        derun
        menu()
    elif choice==0:
        quit
    else:
        print ("That is not a correct selection, please pick either 1, or 2.")

menu()
Reply
#2
A quick glance shows a mix of 'input()' and 'raw_input'. If you are using Python 2.x use 'raw_input()', if you are using Python 3.x, use 'input()'.

In both cases, the variables will be strings, so you must use them as strings, not numbers. So for example in your 'menu' function, you have the comparison of a 'string' with a number:

if choice==1:    # choice is a string, 1 is an integer
you could resolve this by making your numbers into strings:

if choice=="1":    # Note the quotes around the number 1
Another solution would be to get an integer value rather than a string:

choice = int(input("Enter a number: "))
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Thank you for all of your help dude.

I have made those changes and it will won't work.

def decrypt():
    a=input()("Give me the word to decrypt:")
    number=input("What was it shifted by?")
    b=list(a)
    str(b)
    c=[ord(x)for x in(b)]
    d=[]
    for i in c:
        d.append(i-number)
    e=[chr(i) for i in (d)]
    e="".join(e)
    print ("Decryption Successful, your word is ",e,"!")
 
def encrypt():
    a=input()("Give me a word:")
    number=input("Give me a number:")
    b=list(a)
    str(b)
    c=[ord(x)for x in(b)]
    d=[]
    for i in c:
        d.append(i+number)
    e=[chr(i) for i in (d)]
    e="".join(e)
    print ("Your Caesar shifted result (ascii code) is",e,"!")
    print ("Your key is ",number," remember that!")
 
def menu():    
    print ("\n\n\nWelcome to my program")
    print ("What would you like to do?")
    print ("Option 1:Encrypt Word")
    print ("Option 2:Decrypt Word")
    print ("If you would like to quit, press 0.")
    choice=input("Pick your selection: ")
    if choice=="1":
        run=encrypt()
        run
        menu()
    elif choice=="2":
        derun=decrypt()
        derun
        menu()
    elif choice=="0":
        quit
    else:
        print ("That is not a correct selection, please pick either 1, or 2.")
 
menu()
Are you able to repair my code. I would really like to test it.
Reply
#4
Your input on line 15 is wrong, you need the prompt inside the parens for the call to input. Then in line 16, remember that input is giving back a string. You need to convert that to an int before you do the adding on line 22. When you are adding, you need to wrap around the alphabet. For instance "Funky" with number = 5 becomes "Kzsp~". It should be "Kzspd". I'm guessing you will need to make similar fixes to your decrypt function. Also, menu should go in a loop, rather than recursively calling itself. And lines 37 and 41 do nothing, so they can be taken out. And please don't use single letter variable names, it makes your code harder to figure out.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Jun-15-2017, 08:16 PM)as1221 Wrote: Are you able to repair my code.

Yes, but that doesn't help you. If you run your code, you will get errors. You need to post the entire error code between the 'error' code tags. Before posting them, however, look at them and see if can decipher what it is telling you (hint: look towards the end).

To get you started, this:
a=input()("Give me the word to decrypt:")
should be:
a=input("Give me the word to decrypt:")
Remember what I said about strings and numbers. You are asking the user for a 'number', the way you are doing so makes 'number' a string, yet further in your code you are using it as if it is in fact a number

d.append(i - number)
This will fail.

A (very) helpful piece of advise, forget the use of single character variables. Python encourages the use of descriptive variable names: 'a' is not descriptive, 'number' is. It makes your code easier to follow not only for yourself, but others that may read it.

There are other errors, but try and figure them out on your own by looking at the error codes. If you can't, post the errors and we will be glad to help you along.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
Beat me to it, ichabod Big Grin
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
(Jun-15-2017, 09:54 PM)sparkz_alot Wrote: Beat me to it, ichabod Big Grin

Yeah, but your answer was clearer because you took more time on it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cesar Cipher ForsakenDusk 5 408 Apr-07-2024, 04:30 PM
Last Post: Pedroski55
Question Rsa Cipher Paragoon2 3 615 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 480 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Caesar Cipher Help pbrowne 2 2,148 Jun-30-2021, 02:36 PM
Last Post: deanhystad
  Learning Python with a Caesar cipher Drone4four 5 4,775 Nov-21-2020, 07:21 PM
Last Post: bowlofred
  Coding caesar's cipher drewbty 3 2,754 May-16-2020, 10:05 AM
Last Post: DPaul
  Can someone please help me convert this simple C ROT cipher code to Python code? boohoo9 5 3,420 Jun-14-2019, 03:02 PM
Last Post: DeaD_EyE
  Caesar Cypher--- I don't understand why it doesn't work ironsheep 12 5,859 Nov-03-2018, 06:53 PM
Last Post: j.crater
  Newbie: Help with code related to Caesar Cipher jessiblah 2 3,385 May-15-2018, 04:28 PM
Last Post: nilamo
  Problem with caesar cipher lucaron 2 2,934 Feb-05-2018, 05:17 PM
Last Post: lucaron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020