For my caesar implementation in Python, the CS50 IDE keeps telling me that I have a syntax error at the else portion of my for loop as shown below. Any help would be GREATLY appreciated! Haha, thanks in advance for a fresh set of eyes. (:

enter image description here

Here is my code for caesar.py

import cs50
import sys

def main():

    # Validate key: check if number of arguments is 2
    if len(sys.argv) != 2:
        print("Invalid number of command-line arguments. Please try again with an integer.")
        return 1

    # Convert string to int data type
    num = sys.argv[1]
    k = int(num)

    # Prompt and validate user input
    print("plaintext: ", end="")
    p = cs50.get_string()

    # Programme's output
    print("ciphertext: ", end="")

    # Iterate over string and shift plaintext character by key
    # Convert ASCII to AI and back to ASCII

    for i in range(0,len(p)):
        if p[i].isalpha(): # Enciper only if character is alphabetical
            if p[i].isupper():
                print("{}".format(chr((ord(p[i]) - 'A' + k)  % 26)))

            else p[i].islower():
                print("{}".format(chr('a' + (ord(p[i]) - 'a' + k) % 26)))

    return 0

if __name__== "__main__":

It is as easy as replacing else with elif, in python a condition is not allowed after an else, remember that else is executed if the if condition is not fulfilled, if we want to impose a new condition we must use elif:

if condition:
elif condition1:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .