简体   繁体   中英

How to run loop until I get each individual letter

I am working on my Caesar Cipher program and I am running into an issue when I am trying to encrypt my message. The error is 'function is not iterable'. So basically I want to run the for loop until it runs through all the letters in the string.

def message():
        message = input("Enter your message here: ").upper()
    return message
def key():
    while True:
        key = int(input("Enter your shift or key between the numbers of 1-26: "))
        if key >=1 and key<=26:
            return key

def encrypt(message, key):
    output = []
    for symb in message:
        numbers = ord(symb) + 90 - key
        output.append(numbers)
    print(output)

Don't reuse names. Rename the message and key arguments of encrypt to something else.

def encrypt(m, k):
    ...

def main():
    encrypt(message(), key())

You have variables with the same name as your functions and this is causing a conflict when it runs.

Make it clearer which is which.

def message():
    msg = input("Enter your message here: ").upper()
    return msg

def key():
    while True:
        k = int(input("Enter your shift or key between the numbers of 1-26: "))
        if k >=1 and k <=26:
           return k

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM