0

I'm writing a program that takes two inputs, number of lines and number of cheers as input. The number of lines is how many lines the user wants to print out and the number of cheers are in the format that 1 cheer is the word "GO" and two cheers are two "GO" s ...and their is the word "BUDDY" within two neighboring GO's. And each new line has to be indented 3 spaces more then the one before. And this is the program I've come up with:

lines = input("Lines= ")
cheers = input("Cheers= ")
if cheers == 1:
    i = 1
    space = 0
    S = ""
    while i<=lines:
        S=S+(" "*space)+"GO \n"
        i += 1
        space+=3
    print S
else:
    n = 1
    cheer1 = "GO BUDDY "
    cheer2 = "GO"
    space = 0
    while n<= cheers:
        print (" "*space)+(cheer1*cheers)+cheer2
        space+=3
        n += 1

But the problem with this is that it doesn't print out the right number of GO's in the number of cheers. How can I modify my code to fix this problem? This is the output format I want to get :

This is the format of the ouput I want to get

3 Answers 3

1

Often in Python you don't need any loops

lines = int(input('Lines= '))
cheers = int(input('Cheers= '))

line = ' BUDDY '.join(['GO']*cheers)
for i in range(cheers):
     print(' '*(i*3) + line)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are right, actually I was asked specifically to write the code using loops.
1
def greet(lines, cheers):
    i = 0
    line_str = ""
    while i < cheers: # Build the line string
        i += 1
        line_str += "GO" if i == cheers else "GO BUDDY "

    i = 0 
    while i < lines: #Print each line
        print(" "*(i*3) + line_str)
        i += 1

greet(2,1)
greet(4,3)
greet(2,4)

2 Comments

That's exactly the same output as my program. I need as many "GO" words in one line as the number given to 'cheers' ... e.g; for lines = 3 and cheers = 3 output should be "GO BUDDY GO BUDDY GO" ..which has 3 "GO" words.
You can try it now @HogRider123
1

Try this.

def greet(lines, cheers):
    for i in range (lines):
        output = (" ") * i + "Go"

        for j in range (cheers):
            if cheers == 1:
                print output
                break
            output += "Budddy Go"
        print output

Hope this helps.

1 Comment

put if cheer == 1 block inside cheers loop. I'll update the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.