Home > Enterprise >  How do I stop Python from generating a new line each time?
How do I stop Python from generating a new line each time?

Time:02-12

I have created a translator for Morse code in Python and the code works fine except for one problem. When it translates the message into Morse code, it places each character onto a new line. I need the entire translation to stay in one line and I cannot figure out how to make it stay in one line, nor why it is generating a new line each time for each character. Please help! Here is the code:

print("\n\tMenu Options: \n")
print("\t1. Type a phrase and have it translated into Morse Code. \n\t2. Exit program \n")


# Define the main function. 

def main():
    charactersList = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0',
                      '1','2','3','4','5','6','7','8','9','.',',','?','!',' ']

    morse = ['.- ','-... ','-.-. ','-.. ','. ','..-. ','--. ','.... ','.. ','.--- ','-.- ','.-.. ','-- ','-. ','--- ','.--. ','--.- ',
             '.-. ','... ','- ','..- ','...- ','.-- ','-..- ','-.-- ','--.. ','----- ','.---- ','..--- ','...-- ','....- ','..... ',
             '-.... ','--... ','---.. ','----. ','.-.-.- ','--..-- ','..--.. ','-.-.-- ','/ ']
    
    print("\nPlease select a menu option by typing 1 or 2.")
    choice = int(input("\nMenu Choice: "))

    # Start the while loop within the main function.

    while choice != 2:

        if choice == 1:
        
            print("\nPlease type the phrase you would like translated into Morse Code.")
            translatee=input()
            translatee=translatee.lower()
            
            for x in translatee:
                
                if x in charactersList:
                    print("Translation: ",morse[charactersList.index(x)])

            
        else:
            print("Error: Please select a valid menu option.")

        # See what the users' next choices are.

        choice = int(input("\nMenu Choice: "))

    print("\n\tExited.")

# Call the main function.    
    
main() 

input(exit)

Here is an example of the output: screenshot of output

CodePudding user response:

Replace:

for x in translatee:
                
    if x in charactersList:
        print("Translation: ",morse[charactersList.index(x)])

By:

print("Translation:")
for x in translatee:
                
    if x in charactersList:
        print(morse[charactersList.index(x)], end=' ')

to prevent Python to create a new line for each translated character. By default, end='\n' that's why python creates a new line.

CodePudding user response:

Use end= in the print statement and move Translation outside the loop.

if choice == 1:
    
        print("\nPlease type the phrase you would like translated into Morse Code.")
        translatee=input()
        translatee=translatee.lower()
        print("Translation: ") # <-- add print line here
        for x in translatee:
            
            if x in charactersList:
                print(morse[charactersList.index(x)], end=' ') # add end= here

CodePudding user response:

You could accomplish this by keeping track of whether or not this is the first iteration of the for loop using a variable such as firstTime, the first time you enter the for loop you will need to check if firstTime is true and then set it to false and print out the morse code with Translation: at the start, this way nn the next iteration of the for loop the print statement will not print out Translation: at the start. You can stop the print function from automatically ending the line by adding end="" at the end of the print statement. The firstTime variable should be inside of the if statement in the while loop so that it is reset to true every time the user wants to translate morse code.

Here is the new code:

print("\n\tMenu Options: \n")
print("\t1. Type a phrase and have it translated into Morse Code. \n\t2. Exit program \n")


# Define the main function. 

def main():
    charactersList = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0',
                      '1','2','3','4','5','6','7','8','9','.',',','?','!',' ']

    morse = ['.- ','-... ','-.-. ','-.. ','. ','..-. ','--. ','.... ','.. ','.--- ','-.- ','.-.. ','-- ','-. ','--- ','.--. ','--.- ',
             '.-. ','... ','- ','..- ','...- ','.-- ','-..- ','-.-- ','--.. ','----- ','.---- ','..--- ','...-- ','....- ','..... ',
             '-.... ','--... ','---.. ','----. ','.-.-.- ','--..-- ','..--.. ','-.-.-- ','/ ']
    
    print("\nPlease select a menu option by typing 1 or 2.")
    choice = int(input("\nMenu Choice: "))

    # Start the while loop within the main function.
    
    while choice != 2:

        if choice == 1:
            firstTime = True
            
            print("\nPlease type the phrase you would like translated into Morse Code.")
            translatee=input()
            translatee=translatee.lower()
            
            for x in translatee:
                
                if x in charactersList:
                    if firstTime:
                        firstTime = False
                        print("Translation: ",morse[charactersList.index(x)], end="")
                    else:
                        print(morse[charactersList.index(x)], end="")
                        
            
        else:
            print("Error: Please select a valid menu option.")

        # See what the users' next choices are.

        choice = int(input("\nMenu Choice: "))

    print("\n\tExited.")

# Call the main function.    
    
main() 

input(exit)

This is the output when I ran it and told it to translate it works!


    Menu Options: 

    1. Type a phrase and have it translated into Morse Code. 
    2. Exit program 


Please select a menu option by typing 1 or 2.

Menu Choice: 1

Please type the phrase you would like translated into Morse Code.
it works!
Translation:  .. - / .-- --- .-. -.- ... -.-.-- 
Menu Choice: 

  • Related