Hi everyone I am having a little problem with a program that I wrote

def P():
        a = "a"
        aa = "aa"
        b = "b"
        bb = "bb"
        text = raw_input("Enter Text: ")
	SpacedText = ' '.join(list(text))
	OutputText = SpacedText
	if a in OutputText:
		print aa
	if b in OutputText:
		print bb

Now this program works fine but for two things.

1. When I run the program and I am asked to "Enter Text" and I type in "a" the output text is “aa” (same for when I type b). However, when I type in both letter together like this (ba) the output is (aa bb) but what I am looking for is that the output would be in the order I type the letters when I am asked to “Enter Text”.

2. The second problem is that the output is printed diagonally instead of horizontally which is what I would like.

I am sure this errors can be fixed and I would appreciated if anyone could help me with it. Thanks in advance.

8 Years
Discussion Span
Last Post by shadwickman

I'm gonna try looking at how to fix the first problem, but as for the second one, if you use print aa, and print bb, (note the comma at the end), it'll print the aa on the same line as the bb, but will automatically add one space in between them by doing so. The normal print without the comma prints the specified value and then adds a carriage return, so when print is called next time, it'll be on a new line.
Right now though, I'll try to figure out that first issue you're having :)


Phew, I just figured out an answer to your first question. It also easily solves that second one.

def P():
        a = "a"
        aa = "aa"
        b = "b"
        bb = "bb"
        text = raw_input( "Enter Text: " )
        temp = []
        for character in text:
                if character == "a":
                        temp.append( aa )
                if character == "b":
                        temp.append( bb )
        OutputText = " ".join( temp )
        print OutputText

This takes the inputted string, and goes though each character in it. When it finds an "a" or a "b", it adds the values "aa" or "bb" (respectively) to a list (temp). Then, it just adds all the items in this list together with a space inbetween each of them. This new string (OutputText), in the end gets printed out. Hope that helps :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.