Python Forum
Help with getting sub-program to print text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with getting sub-program to print text
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 751 Apr-18-2024, 07:07 AM
Last Post: Bronjer
  Start print a text after open an async task via button Nietzsche 0 706 May-15-2023, 06:52 AM
Last Post: Nietzsche
  Saving the print result in a text file Calli 8 1,793 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Print text with big font and style tomtom 5 14,081 Mar-03-2022, 01:29 AM
Last Post: tomtom
  saving and loading text from the clipboard with python program MaartenRo 2 1,659 Jan-22-2022, 05:04 AM
Last Post: MaartenRo
  UnicodeEncodeError caused by print when program runs from Popen SheeppOSU 5 2,921 Jan-13-2022, 08:11 AM
Last Post: SheeppOSU
  read terminal text from running program AArdvark_UK 2 1,889 Aug-27-2020, 12:43 PM
Last Post: AArdvark_UK
  Want to print each iteration of factorial program sbabu 10 4,580 Jan-09-2020, 07:25 AM
Last Post: perfringo
  Print to Text file in Python 3 iteachpc 2 2,347 May-02-2019, 04:38 AM
Last Post: perfringo
  Why does function print require bracket for text? richardm55 2 2,977 May-23-2018, 07:30 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020