0

Currently, I'm working on a Caesar Cipher program for my Computer Science class. I however don't know how to use user-defined functions in this situation. I keep receiving an UnboundLocalError

#user defined functions

def encrypt(message, distance):
    """Will take message and rotate it the distance, in order to create an encrypted message"""


    for ch in message:
        ordvalue = ord(ch)
        cipherValue = ordvalue + distance
        if cipherValue > ord("z"):
            cipherValue = ord("a") + distance - (ord("z") - ordvalue + 1)
        encryption += chr(cipherValue)
        return encryption

#input 

message = input("Enter word to be encrypted: ") #original message
distance = int(input("Enter the distance value: ")) #distance letters will be moved

# test
fancy = encrypt(message, distance)

#encryption

print(fancy)
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • 2
    What's the exact error? Where is `encryption` declared? – Carcigenicate Mar 02 '19 at 22:01
  • 2
    Regardless of the error, you have `return` in a `for` loop which is basically pointless because it will immediately break out of the function. On another theme, the fact that your assignment is due on Monday is _your_ concern, not the concern of Stack Overflow or any other readers of this question. This is not a help desk to serve you, it's supposed to be a repository of general questions and answers helpful to all. – roganjosh Mar 02 '19 at 22:03
  • Your problem seems to be unrelated to *user defined functions*, its just unfinished code with some deficiencies. – guidot Mar 02 '19 at 22:06
  • 1
    Possible duplicate of [Don't understand why UnboundLocalError occurs](https://stackoverflow.com/questions/9264763/dont-understand-why-unboundlocalerror-occurs) – ivan_pozdeev Mar 02 '19 at 22:12

2 Answers2

0

You are accessing variable encryption without declaring it. Declare it as an empty string before the for loop to prepare it for adding partial strings to it.

See more discussion in a related discussion on this site here.

On another note, return statement in you function is indented in the for loop, which means it will exit the function after one iteration of the loop, making the loop pointless. This looks like a mistake and it should be indented one level less.

m1n0
  • 609
  • 5
  • 16
0

The reason you get the UnboundLocalError is because you reference encryption before defining it when you write encryption += chr(cipherValue) since encryption is never defined in the first place. To fix this you can define it (as an empty string) at the start of your function.

Also you need to move the return statement out one block so that it goes after the loop, not inside of it.

Here's an example:

def encrypt(message, distance):
    """Will take message and rotate it the distance, in order to create an encrypted message"""
    encryption = ""
    for ch in message:
        ordvalue = ord(ch)
        cipherValue = ordvalue + distance
        if cipherValue > ord("z"):
            cipherValue = ord("a") + distance - (ord("z") - ordvalue + 1)
        encryption += chr(cipherValue)
    return encryption
Henry Woody
  • 14,024
  • 7
  • 39
  • 56