Python Forum

Full Version: Help with getting sub-program to print text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to use the following sub-program to print a shift code:
def shift():
    message = input("Please enter a message:")
    number = int(input('Please enter the number that you would like to shift by (1-26):'))
    new_message = ""
    for letter in message:
        letter = letter.lower()
        if letter.isalpha():
            new_position = alphabet.index(letter) + number
            if new_position > 25:
                new_position = new_position - 26
            new_letter = alphabet[new_position]
        elif letter.isnumeric():
            letter = letter
            print(letter)
        elif letter == " " or letter == "," or letter == "." or letter == ";" or letter == ":":
            letter = letter
            print(letter)
        else:
            print("Error in message please try a different message")
    print(new_message)
    print()
But its just printing blank space, can someone help me with the reason it;s doing this?
What have you done to debug your code? Add some extra print statements to see what's going on: print more of your variables to see what their values are and add prints in your if, elif and else branches to see which ones are being taken. Both of those should help you work out what's going wrong.
If letter.isalpha() nothing will print. If you have other characters in message they are printed.

I think your problem is new_message is an empty str (""). Your code never adds any letters to it.

I think you should not write your program in Python. You don't know Python. You need to write your program in some other language (like pseudocode) and translate to Python. You are missing obvious details, like not recording the translation in new_messsage. I think that happens because your mind is focusing on syntax and not on program design.

Eventually you will be comfortable enough with Python that you can directly code simple programs. Maybe when you master the language you can directly go from idea to code for larger programs. I don't know as I have not reached that level of mastery in any programming language.