-1

I have to do a hash triangle using this function:

def line(num=int, word=str):
   if word == "":
       print("*" * num)
   else:
       print(word[0] * num)

Here is the code i have:

def line(num=int, word=str):
    if word == "":
        print("*" * num)
    else:
        print(word[0] * num)

def triangle(size):
    # You should call function line here with proper parameters
    
    while size > 0:
        
        line(size, "#")
        size -= 1

the output is this:

######
#####
#### 
###  
##   
# 

but it should be this:

#
##
###
####
#####
######

how can i modify it so it prints that?

Wolfie
  • 11
  • 2
  • Think carefully about the logic of the code. `i` controls the number of `#` symbols that will be printed on each line, right? So, if you want the first line to have one symbol, what should `i` be equal to the first time through the loop? Therefore, how should we set `i` before the loop? Should the number of symbols on each line increase, or decrease? Therefore, what should happen to `i` each time through the loop? When we are done with the triangle, what can we say about the value of `i`? Therefore, how can we control the loop to finish the triangle? – Karl Knechtel Sep 21 '22 at 23:58
  • Please try to solve problems yourself before asking. This is not a tutoring service. Programming is fundamentally about thinking. – Karl Knechtel Sep 21 '22 at 23:58

2 Answers2

-1

You are starting from size and going to 1 you should do the opposite:

i = 0
while i <= size:
    i += 1
    ...

Also for i in range(1, size + 1) would be more pythonic ;)

yjay
  • 942
  • 4
  • 11
-1

Slightly adjusted your code

def line(num=int, word=str):
  if word == "":
    print("*" * num)
  else:
    print(word[0] * num)

def triangle(size):
  # You should call function line here with proper parameters
  charPerLine = 1
  while charPerLine <= size:
      line(charPerLine, "#")
      charPerLine += 1
        
        
triangle(5)
Gro
  • 1,613
  • 1
  • 13
  • 19