How to run loop until I get each individual letter

时间:2016-10-20 13:03:04

标签: python loops

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)

2 个答案:

答案 0 :(得分:0)

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

def encrypt(m, k):
    ...

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

答案 1 :(得分:0)

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.