#🔒 why does my code not work

100 messages · Page 1 of 1 (latest)

rapid ruin
#

im trying to encrypt and decrypt the Caesar encryption but im stuck at this point and idk how to continue :(((

the code:

#verschlüsselung
def caesarEnc(Klartext, Key):
    geheimtext=""
    klartext=klartext.upper()
    for buchstabe in klartext:
        unicode=ord(buchstabe)
        unicode+=key
        if unicode>90:
            unicode-=26
        geheimbuchstabe=chr(unicode)
        geheimtext+=geheimbuchstabe
        return geheimtext
#entschlüsselung    
def caesarDec(geheimtext, key):
    klartext=""
    geheimtext=geheimtext.upper()
    for buchstabe in geheimtext:
        unicode=ord(buchstabe)
        unicoded-=key
        if unicode<65:
            unicode+=26
        klartext=chr(unicode)
        klartext-=klarbuchstabe
        return klartext
        
    
    
# text="DIE SPINNEN DIE GALLIER!"
text="CaesarZ"
key=3
textEnc=caesarEnc(text, key)
textDec=caesarDec(textEnc, key)
print("Original: "+text.upper())
print("Verschlüsselt: "+textEnc)
print("Entschlüsselt: "+textDec)
caesarBreak(textEnc)```
tranquil orbitBOT
#

@rapid ruin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tardy jay
#

how about to show an error?

#

but from what i see right now one of your arguments is Klartext but inside function you use klartext

rapid ruin
#

oh

#

this is my error:

#

%Run 'von Lets code Python.py'
Traceback (most recent call last):
File "C:\Users\alexi\Downloads\von Lets code Python.py", line 32, in <module>
textDec=caesarDec(textEnc, key)
File "C:\Users\alexi\Downloads\von Lets code Python.py", line 23, in caesarDec
klartext-= klarbuchstabe
NameError: name 'klarbuchstabe' is not defined

tardy jay
#

just learn to read errors here it says you have problem in caesarDec
and yeah you have mistake in lines

        klartext=chr(unicode)
        klartext-=klarbuchstabe
rapid ruin
#

but i dont undeerstand what my mistake there is

tardy jay
#

compare this lines with similar lines in caesarEnc

rapid ruin
#

ok wait

#

nio sry i dont get it

#

no*

tardy jay
#

ok firstly lets get back to error
it says NameError: name 'klarbuchstabe' is not defined
as you can see in you code klartext-=klarbuchstabe
but klarbuchstabe was never defined
compare it with lines in caesarEnc:

        geheimbuchstabe=chr(unicode)
        geheimtext+=geheimbuchstabe
rapid ruin
#

so the "geheimbuchstabe=chr(unicode)
geheimtext+=geheimbuchstabe" is a definition of geheimbuchstabe and i was supposed to do the same with klarbuchstabe?

tardy jay
#

yes, btw do you understand what this code do or you just copy/paste?

rapid ruin
#

no i wrote it myself but with specifications from my teacher

#

its still not working even thought i did change klarbuchstabe

#
#entschlüsselung    
def caesarDec(geheimtext, key):
    klartext=""
    geheimtext=geheimtext.upper()
    for buchstabe in geheimtext:
        unicode=ord(buchstabe)
        unicode-=key
        if unicode<65:
            unicode+=26
        klarbuchstabe=chr(unicode)
        klartext-= klarbuchstabe
        return klartext```
tardy jay
#

because when you write Decode function you are not supposed do everything opposite. there is still mistake. Try to use error message to find it

rapid ruin
#

ok

#
# text="DIE SPINNEN DIE GALLIER!"
text="CaesarZ"
key=3
textEnc=caesarEnc(text, key)
textDec=caesarDec(klartextEnc, key)
print("Original: "+text.upper())
print("Verschlüsselt: "+textEnc)
print("Entschlüsselt: "+textDec)
caesarBreak(textEnc)```
#

i changed this and now there is one error less

#

but the last one i dont understand

tardy jay
#

you probably find it wrong, or you have klartextEnc somewhere defined?

rapid ruin
#

oh no its worse if i change this --^ it makey there stand only one error that klartext is again not defined but if i change it back to text there are errors i dont understand

#

oh makes sense

tardy jay
#

can you show me original error message so i explain

rapid ruin
#

sure

#

sry for quality

#

oh iget it

tardy jay
#

OK, firstly this is one report of one error
so first part that points on caesarDec actually just says that error occurred during call of function caesarDec

The second part is pointing klartext-= klarbuchstabe which is actually problem

rapid ruin
#

i had to make the - to an + by klartext bc i change unicode to letters

tardy jay
#

each but explanation is not correct

rapid ruin
#

and the bottompart this one # text="DIE SPINNEN DIE GALLIER!"
text="CaesarZ"
key=3
textEnc=caesarEnc(text, key)
textDec=caesarDec(klartextEnc, key)
print("Original: "+text.upper())
print("Verschlüsselt: "+textEnc)
print("Entschlüsselt: "+textDec)
caesarBreak(textEnc)

#

is copy and paste wich is why the code aint working

#

bc of the last line

tardy jay
#

i still think that idea is to

textEnc=caesarEnc(text, key)
textDec=caesarDec(textEnc, key)

you trying encode then decode, right?

tardy jay
rapid ruin
#

yes

#

fixed it

#

i peeked into my teachers solution for it and there is this standing, do u know what its suppost to do bc i dont have i ```py
def caesarBreak(geheimtext):
for i in range(1,26):
print("Key:",i, "Text:",caesarDec(geheimtext, i))

tranquil orbitBOT
#

Hey @rapid ruin!

Please edit your message to use a code block

Make sure you put your code on a new line following py. There must not be any spaces after py.

Here is an example of how it should look:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
tardy jay
#

okey but you explained it not correctly
when you use klartext += klarbuchstabe
its not related to unicode klartext is your buffer to store result and with += you just add to result next letter
(error occurs because strings dont have -, -= operations)

rapid ruin
#

so it connects my letters?

tardy jay
tardy jay
rapid ruin
#

ohhh ok makes sense now

rapid ruin
rapid ruin
#

but the last part i a mystery to me

tardy jay
rapid ruin
#

?

#

the i

tardy jay
#

its named i but in print

print("Key:",i, "Text:",caesarDec(geheimtext, i))

you can see that it also mentioned as Key in output

#

ok let say print("Key: ", i) for now do you understand what this do?

rapid ruin
#

no

#

:(

tardy jay
#

so in print you can put as many argument as you want to print
so for example your code

print("Original: "+text.upper())

can be rewriten as

print("Original:", text.upper())
rapid ruin
#

okay

tardy jay
#

also note that print("Original:", text.upper()) will put space between all arguments automaticaly

rapid ruin
#

wait why does it do that

tardy jay
#

ok so returning back to

print("Key:",i, "Text:",caesarDec(geheimtext, i))

It will print

Key: <value_of_i> Text: <result_of_caesarDec>
tardy jay
rapid ruin
#

why does it put space between all arguments

tardy jay
#

One sec, ill edit it to strings

rapid ruin
#

ok so if it wasnt there it would just be ab

tardy jay
rapid ruin
#

Key: <value_of_i> Text: this yes the other no

tardy jay
#

okay so in print

print("Key:",i, "Text:",caesarDec(geheimtext, i))

we called function that decodes
its allowed
it means that program will run caesarDec(geheimtext, i) first and use result of that function in print

#

it like evaluating an expression with parentheses in math (you evaluate result in parentheses first)

rapid ruin
#

okay, so it will run through the keys from the text, and then find results for the variable i so that geheimtext becomes klartext

#

sry for taking long for answeres i only got 10 mins left bevore i have to go take the exam

#

(i do this code over and over in thonny)

tardy jay
#

you got idea of function correct but not how its implemented
it will literaly try to decode with every 26 key, and print all results, so code itself dont know what is correct key, but just showing all decode variants (if initial text is readable then human will able to detect correct result)

rapid ruin
#

So thats why it aint writing the thing in a sentence but like this

tardy jay
#

seems not correct what is your geheimtext?

rapid ruin
#

text="DIE SPINNEN DIE GALLIER!"

#

do u think i can use my formula collection in the exam?

tardy jay
#

what is formula collection?

#

you have another mistake that i didnt noticed

rapid ruin
#

its a collection of basic knowledge whis we can usually use in math physics and chem exams

#

oh no

tardy jay
#

in encode in decode functions
you need move return on one block left

rapid ruin
#

u can continue to text but igtg, ic caan continue reading ur messages in the train

tardy jay
#

so its

def caesarEnc(Klartext, Key):
    geheimtext=""
    klartext=klartext.upper()
    for buchstabe in klartext:
        unicode=ord(buchstabe)
        unicode+=key
        if unicode>90:
            unicode-=26
        geheimbuchstabe=chr(unicode)
        geheimtext+=geheimbuchstabe
    return geheimtext # moved return

before that you had problem because your code was encoding only first letter (thats why you can see only one letter in your console)

rapid ruin
#

The return geheimtext is new

#

Do I always have to use it if i want the full sentence decodet

tardy jay
#

before was problem that return was inside loop so its like start loop do 1 cycle and return result

but it should run loop fully and after that return result

tardy jay
rapid ruin
#

Okay

#

Since this problem seems solved

#

THANKYOUSMFORHELPINGME

#

U are very good at making ppl understand things

tranquil orbitBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.