Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program that reads strings typed by user until nothing entered then prints strings in reverse order

I am trying to write a program that allows a user to continue to type out strings until an empty string is entered. Then the program sorts through the strings and prints the string in depending order ( lexicographically ).

The string output should look something like this.

enter a string: the tree is nice and brown
output: tree the nice is brown and 

I have tried to implement this code for myself however I have ran into a problem where it prints the code multiple times. see bellow code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Enter string: the tree is nice and brown
Output:  tree Output:  the Output:  nice Output:  is Output:  brown Output:  and Enter string: 

how can I fix my code to remove the Output: that continues to be printed after each word in the string. And also make the final enter new string print on a new line.
See bellow my final code.

s=input("Enter string: ")

while s!="":
    a=s.lower()
    b=a.split()
    b.sort(reverse=True)
    for i in b:
        answer=""
        answer+=i
        print("Output: ", answer, end=" ")
    s=input("Enter string: ")

>Solution :

Does this fix the problem.

s=input("Enter string: ")

while s!="":
    a=s.lower()
    b=a.split()
    b.sort(reverse=True)
    output = ("OUTPUT: \n" + " ".join(b)
    print(output)
    s=input("Enter string: ")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading