Reputation: 154
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 :
Upvotes: 0
Views: 40
Reputation: 476
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.
Upvotes: 1
Reputation: 1750
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)
Upvotes: 1
Reputation: 564
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)
Upvotes: 1