Python Forum

Full Version: Caesar Cypher--- I don't understand why it doesn't work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
alphabet= "abcdefghijklmnopqrstuvwxyz"
cleartext= input()
cleartext=cleartext_lowercase()
def encrypt(cleartext):
codedtext = " "
for character in cleartext:
if character in alphabet:
newcharacter= (alphabet.find(character) + 13) %26
codedtext += alphabet[newcharacter]
elif:
codedtext += character
return codedtext
print(encrypt(cleartext))






This is my python code for a caesar cipher I made after watching the Youtube Channel KidsCanCode Lesson 1.10. I already asked Chris Bradfield for help and thanks to his input, I fixed the errors he told me, but I still have errors. I don't want to bug him by asking him again to look at why this code doesn't work. My goal is to type in sheep and have the secret message as furrc. I recommend that you watch Chris Bradfield's video for clarification.
Please put your code in Python code tags, and full error traceback message in error tags. You can find help here.
I am still confused on how to do this-- I am new to python

I clicked on your link, but why can't I upload the python file like I did on the email I sent to Chris?
Just copy and paste your Python code (like you did in your original post), but enclose it in [python] tags. Docs page explains it quite clearly, here is an image of how it looks.
And do same with error traceback message, just use [error] tags.
Simplest way is to use the icons on the toolbar.
You can see a Python logo (click it for Python tags) and red circled cross (click it for error tags). And put the respective content between them.
alphabet= "abcdefghijklmnopqrstuvwxyz"
cleartext= input()
cleartext=cleartext_lowercase()
def encrypt(cleartext):
    codedtext = " "
    for character in cleartext:
            if character in alphabet:
                newcharacter= (alphabet.find(character) + 13) %26
                codedtext += alphabet[newcharacter]
            elif:
                codedtext += character
            return codedtext
print(encrypt(cleartext))
This is my python code for a caesar cipher I made after watching the Youtube Channel KidsCanCode Lesson 1.10. I already asked Chris Bradfield for help and thanks to his input, I fixed the errors he told me, but I still have errors. I don't want to bug him by asking him again to look at why this code doesn't work. My goal is to type in sheep and have the secret message as furrc. I recommend that you watch Chris Bradfield's video for clarification.

Just run the program by typing in the word sheep-- and you will get syntax error( even though elif: is cif correct), why does python not accept elif. My goal is to cesear cipher the word sheep.

How are you writing with/in a green bar??

Also, why does it say Unladen Swallow under my username???
(Nov-01-2018, 01:17 AM)ironsheep Wrote: [ -> ]
Just run the program by typing in the word sheep-- and you will get syntax error( even though elif: is cif correct), why does python not accept elif. My goal is to cesear cipher the word sheep.

How are you writing with/in a green bar??

Also, why does it say Unladen Swallow under my username???

It is not that we can't run the code ourselves. But rather that we shouldn't have to, since you can provide us with the information we need to help you solve the issue. It is not too much to ask for, when giving out free help, is it? ;)

The green bar is an information message cloud a moderator can add to a post.

About Unladen Swallow - we like to be funny here sometimes, so we thought of user titles which reference things appearing in Monty Python sketches/films. You can read about it here.


And since you have already revealed where the syntax error happens...
elif requires an expression to be evaluated, just like a "regular" if.
If you want a default case when none of previous if/elif conditions are satisfied, you should use "else".
alphabet= "abcdefghijklmnopqrstuvwxyz"
cleartext= input()
cleartext=cleartext_lowercase()
def encrypt(cleartext):
    codedtext = " "
    for character in cleartext:
            if character in alphabet:
                newcharacter= (alphabet.find(character) + 13) %26
                codedtext += alphabet[newcharacter]
            else:
                codedtext += character
            return codedtext
print(encrypt(cleartext))
I replaced the elif: with else and now my new error message is
Error:
Traceback (most recent call last): File "C:\Users\Mitesh Patel\Desktop\secret code.py", line 3, in <module> cleartext=cleartext_lowercase() NameError: name 'cleartext_lowercase' is not defined
The error message contains all the information you need. The variable clearrext_lowercase is indeed not defined as you have only used it in order to transfer its contents to cleartext without previously defining it or giving it a value. It is this that Python is objecting to.
I suspect that you are trying to ensure that anything entered via the input() statement is lowercase. To do this, the line should be change to:
cleartext=cleartext.lower()
which means that the lower() method of the string type is used on cleartext, and the result is stored in cleartext.
Making this change will still not give you exactly what you expect, but have a go at getting your program to work correctly now.
Thank you for the clarification--- I don't understand why the computer doesn't understand what I meant with cleartext_lowercase(). You understood what I was trying to do, how come computers can't?? What is with the computer and it's thinking??

alphabet= "abcdefghijklmnopqrstuvwxyz"
cleartext= input()
cleartext=cleartext.lower()
def encrypt(cleartext):
    codedtext = " "
    for character in cleartext:
            if character in alphabet:
                newcharacter= (alphabet.find(character) + 13) %26
                codedtext += alphabet[newcharacter]
            else:
                codedtext += character
            return codedtext
print(encrypt(cleartext))
Now when i run the program with sheep as my input, all I get is f---- the other letters are not showing up. f goes with s, but what about heep?? This should be working because I have codedtext+=alphabet[newcharacter].
(Nov-02-2018, 09:42 PM)ironsheep Wrote: [ -> ]You understood what I was trying to do, how come computers can't??

You told us more or less, what you're trying to do, but you have to explain it to the computer.

To answer your question:
Look at line 12!
Pages: 1 2