Home > database >  Why is this function just being skipped and not called?
Why is this function just being skipped and not called?

Time:11-01

I can't seem to see why this doesn't print each item in the loop. I ran it through Thonny and it just completely skips my function. Am I not passing in the variables/arguments correctly?

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))


def encrypt(direction, text, shift):
    if direction == "encode":
        for index, letter in enumerate(text):
            print(letter)

I feel like I am messing up passing the values in from outside the function.

CodePudding user response:

All you've done is define the function. You need to call it like

encrypt(direction, text, shift)

Right after you have gotten the input for those variables. Calling a function also needs to be done after it's defined, so you should move the function definition up at the top of the program.

Putting the names of parameters in the function definition doesn't "link" those to any variables named that in the rest of the program. Those parameters take on the value of whatever is passed in to the function. So for example, you could do

encrypt("encode", "hello", 3)

and then inside the function direction would be "encode", text would be "hello", and shift would be 3.

  • Related