-4

Q. Write a function called shift_string that takes a string and an integer n as parameters and returns a new string with every letter of the string shifted by the n alphabets. It should also work for negative alphabets in reverse order.

So far, I have come up with this:

usr_input=input('Please enter string: ')
n=input('enter shifts: ')
encryption= ""

def shift_string(usr_input,n):
    for i in usr_input:
        if i.isupper():
            i_unicode=ord(i) #find position
            
            i_index=i_unicode-ord('A')
            
            new_index= (i_index + n)
            
            new_unicode= new_index +ord('A') #to perform shift
            
            new_character=chr(new_unicode)
            encryption= encryption+new_character #to append string
        else:
            encryption=encryption+i #for non-uppercase
print('Encrypted text is',encryption)

At encryption= encryption+new_character I am getting the error:

"Local variable 'encryption' defined in enclosing scope on line 23 referenced before assignment...(pyflakes E)"

ZAA
  • 1
  • 3
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). You get one question per post. – Prune Mar 11 '21 at 19:19
  • Your last paragraph is quite out of scope here. "I'm stuck" is not a Stack Overflow issue. ["Can Someone Help Me?" is not a valid SO question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). This suggests a collection of needs that are too broad for Stack Overflow. – Prune Mar 11 '21 at 19:21
  • Your code doesn't produce the second error; you never call the function. – Prune Mar 11 '21 at 19:25
  • Does this answer your question? [Python overwriting variables in nested functions](https://stackoverflow.com/questions/7935966/python-overwriting-variables-in-nested-functions) – Adrian W Mar 11 '21 at 21:42

1 Answers1

0
n=input(eval('enter shifts: '))

The argument to eval() has to be a string containing a Python expression. enter shifts is not a valid expression, you can't evaluate it. I suspect you meant eval(input('enter shifts: ')).

But you shouldn't use eval() to process user input. If you want to convert the input to a number, use int() or float().

n = int(input('enter shifts: '))

The second error is because encryption is a local variable in the function, and you're trying to add to it before you've initialized it. You need to move encryption = "" inside the function. Then you can return the value.

def shift_string(usr_input,n):
    encryption = ""
    for i in usr_input:
        if i.isupper():
            i_unicode=ord(i) #find position
            
            i_index=i_unicode-ord('A')
            
            new_index= (i_index + n)
            
            new_unicode= new_index +ord('A') #to perform shift
            
            new_character=chr(new_unicode)
            encryption= encryption+new_character #to append string
        else:
            encryption=encryption+i #for non-uppercase
    return encryption

encrypted = shift_string(usr_input, n)
print('Encrypted text is',encrypted)
Barmar
  • 741,623
  • 53
  • 500
  • 612