im trying to create a variable outside of a def, bring that variable into a def change it, then use the modified variable
<p>the code is going to be kind of long, but I'm still new and learning how to compress code.
The gist of what im trying to do is</p>

<pre><code>L1 = c

def code():
    let = d

let = L1
code
L1 = let

print (let)
</code></pre>

<p>I have lots more code involved but the gist of what actually happens in <code>def code():</code> is whatever <code>let</code> equals (which will be a letter) will become the next letter. That all works if I do it for each letter but that makes the code extremely long since whats in code() is about 15 lines long.</p>

<p>The point of the program is to code a word or small phrase. So if <code>L1=a</code> then <code>def code():</code> would bring it too the next letter in the alphabet unless it is the same as one of the letters in <code>key</code>. then it will keep going up one letter until it cant be the same as a letter in <code>key</code>. unless <code>L1</code> equals a letter in <code>key</code> from the start in which case, it will stay the same, then repeats that 14 more times.</p>

<p>if you would like to see what i mean, this is my actual code.</p>

<pre><code>key = ["r", "a", "b", "b", "i", "t"]

#Enter word to code
print("Welcome to Part 2")
print("Type each letter of your word followed by enter")
print("Type the 15 letters (or less) word to code.")
print("(if word is shorter, leave those spaces blank.)")

#Code Word
L1 = input ("First Letter: ").lower()
L2 = input ("Second Letter: ").lower()
L3 = input ("Third Letter: ").lower()
L4 = input ("Fourth Letter: ").lower()
L5 = input ("Fifth Letter: ").lower()
L6 = input ("Sixth Letter: ").lower()
L7 = input ("Seventh Letter: ").lower()
L8 = input ("Eighth Letter: ").lower()
L9 = input ("Ninth Letter: ").lower()
L10 = input ("Tenth Letter: ").lower()
L11 = input ("Eleventh Letter: ").lower()
L12 = input ("Twelfth Letter: ").lower()
L13 = input ("Thirteenth Letter: ").lower()
L14 = input ("Fourteenth Letter: ").lower()
L15 = input ("Fifteenth Letter: ").lower()

#combining all the letters into one word if you want to see the word before it codes
#code = L1+L2+L3+L4+L5+L6+L7+L8+L9+L10+L11+L12+L13+L14+L15

#Coding the letters

#making blank lines
print()
print()

def code():
    if let in key:
        let = let

    else:
        let = let.translate(bytes.maketrans(b"abcdefghijklmnopqrstuvwxyz",b"bcdefghijklmnopqrstuvwxyza"))

        if let in key:
            let=let.translate(bytes.maketrans(b"abcdefghijklmnopqrstuvwxyz",b"bcdefghijklmnopqrstuvwxyza"))

            if let in key:
                let=let.translate(bytes.maketrans(b"abcdefghijklmnopqrstuvwxyz",b"bcdefghijklmnopqrstuvwxyza"))

                if let in key:
                    let=let.translate(bytes.maketrans(b"abcdefghijklmnopqrstuvwxyz",b"bcdefghijklmnopqrstuvwxyza"))

                    if let in key:
                        let=let.translate(bytes.maketrans(b"abcdefghijklmnopqrstuvwxyz",b"bcdefghijklmnopqrstuvwxyza"))

                        if let in key:
                            let=let.translate(bytes.maketrans(b"abcdefghijklmnopqrstuvwxyz",b"bcdefghijklmnopqrstuvwxyza"))

                            if let in key:
                                let=let.translate(bytes.maketrans(b"abcdefghijklmnopqrstuvwxyz",b"bcdefghijklmnopqrstuvwxyza"))

                            else:
                                let = let

                        else:
                            let = let

                    else:
                        let = let

                else:
                    let = let

            else:
                let = let

        else:
            let = let         


let = L1
code
L1= let

let = L2
code
L2 = let

let = L3
code
L3= let

let = L4 
code
L4= let 

let =  L5 
code
L5= let 

let =  L6 
code
L6= let 

let =  L7 
code
L7= let 

let =  L8 
code
L8= let 

let =  L9 
code
L9= let 

let =  L10 
code
L10= let 

let =  L11 
code
L11= let 

let =  L12 
code
L12= let 

let =  L13 
code
L13= let 

let =  L14 
code
L14= let 

let =  L15 
code
L15= let 

code = L1+L2+L3+L4+L5+L6+L7+L8+L9+L10+L11+L12+L13+L14+L15
print(code)
</code></pre>

<p>that's all my code. Now imagine whats in <code>def code():</code> was written for L1-L15 with <code>let</code> being L1-L15 respectively. </p>

<p>I have a different version which works but it is the version with <code>code()</code> written out for L1-L15.</p>

<p>I'm very sorry if i made no sense. i'm not very sure how to explain what i'm saying. Thanks for the help.</p>

<p>This is rather confusing, so I may not have the right answer here, but I'll take a shot.  First, a couple of notes for you.</p>

<p>This <code>def code()</code> is normally called a function.  It sounds like you want to pass variables into and out from your function, which you can do with parameters.  Here's an example:</p>

<pre><code>def code(number, key):
    if number == key:
        number += 1
    return number

a_key = 5
a_number = 5
a_number = code(a_number, a_key)
print(a_number)

&gt;&gt;&gt; 6
</code></pre>

<p>You can also clean up your function quite a bit.  Those else statements seem to do nothing (just assign a variable to itself, so it doesn't change), so we can just remove them.  Also, we can improve readability greatly by using another function as a helper, like so:</p>

<pre><code>...
# just imagine all your other code is up here
def helper_function(letter):
    return letter.translate(bytes.maketrans(b"abcdefghijklmnopqrstuvwxyz",b"bcdefghijklmnopqrstuvwxyza"))

def code(let, key):
    if let in key:
        let = let
    else:
        let = helper_function(let)
        if let in key: 
            let=helper_function(let)
            if let in key:
                let=helper_function(let)
                if let in key:
                    let=helper_function(let)
                    if let in key:
                        let=helper_function(let)
                        if let in key:
                            let=helper_function(let)
                            if let in key:
                                let=helper_function(let)
    return let

let = L1
let = code(let, key)
L1 = let
</code></pre>

<p>I may be wrong about what you're trying to do, but these last 3 lines here you might be able to change to</p>

<pre><code>L1 = code(L1, key)
</code></pre>

<p>because it looks like you're only using <code>let</code> as a placeholder for a moment.</p>

<p>I hope that helps.  Keep working at it!</p>

python function variables