0

I am wondering where it has gone wrong. Suppose I want to obtain:

print(triangle(5)) 

*
 * 
* *
 * *
* * *

My code:

def triangle(n):
    result = ""
    for i in range(n):
        if (i+1) % 2 == 1:
            result = result + "*\n"
        if (i+1) % 2 == 0:
            result = " " + result
    return result

and it gives me only

  *
*

( Answer is below, but I'm wondering what went wrong with my own code)

def triangle(n):
    if n == 0:
        return ''
    elif n % 2 != 0:
        return triangle(n-1) + ((n-1)//2)*'* ' + '*\n'
    else:
        return triangle(n-1) + (n//2)*' *' + '\n'

3 Answers 3

1

To break this down...

  • The line result = result + "*\n" will only ever add one star per line. If you compare this to the model answer, it multiplies ' *' by (n/2), which prints a number of stars equivalent to half of n, and then appends an "\n" to insert a new line.

  • Your other case, it looks like you're attempting to add the previous line but then indent it. However, what you're actually doing is re-drawing the entire triangle so-far with a single indent. So what happens is for the first few iterations of the loop you're just printing spaces (see the two spaces before your first *). This would need to change to print the correct number of *'s using n.

The model answer itself uses recursive calls in order to delegate printing other lines, meaning the triangle function's bulk can focus on printing a single line. To put this into an iterative function, like yours, would work like so:

def triangle(n):
  if n == 0:
    return ""

  result = ""
  for i in range(1, n + 1):
    if i % 2 != 0:
      result += (i / 2) * "* "
      result += "*"
    else:
      result += (i / 2) * " *"
    result += "\n"

  return result
Sign up to request clarification or add additional context in comments.

Comments

1

Think about result, when you run with n == 5, through your original for loop.

i   result
-   ------
0   "*\n"
1   " *\n"
2   " *\n*\n"
3   "  *\n*\n"
4   "  *\n*\n*\n"

Which, when printed, is:

  *
*
*

You either add one space to the start, or an asterisk and newline to the end, depending on whether i is odd or even (in fact, whether i+1 is odd or even, which seems unnecessarily complex). That's all your code does.

A better question might be: why did you expect the first code to work?

Comments

1

The "answer" is equivalent to the following code that you can more easily compare with your own:

def triangle(n):
    result = ""
    for i in range(n):
        if (i + 1) % 2 == 1:
            result = result + (i//2)*"* " + "*\n"
        if (i + 1) % 2 == 0:
            result = result + ((i + 1) // 2)*" *" + "\n"
    return result

(For whatever it's worth, I would write something different from all of these. You see, there's more than one way. People who write Python programs eventually read that "there should be one, and preferably only one, obvious way to do it." There are still many ways to do it that are not obvious.)

Comments

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.