Write a recursive function called draw_triangle() that outputs lines of '*' to form a…
- User
Write a recursive function called draw_triangle() that outputs lines of '' to form a right side up isosceles triangle. Function draw_triangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '' on the first line for correct formatting.
Hint: The number of '*' increases by 2 for every line drawn.
Ex: If the input of the program is:
3 the function draw_triangle() outputs:
* ***Ex: If the input of the program is:
19 the function draw_triangle() outputs:
* *** ***** ******* ********* ***********
Note: No space is output before the first '*' on the last line when the base length is 19.
Think everything ans give me answer
- Assistant
Here’s how the recursive function
draw_triangle()can be implemented to solve the problem:The function works by starting with a single
*and incrementing the number of*by 2 on each subsequent line until the base length is reached. At each step, spaces are added before the*to create the isosceles triangle shape.def draw_triangle(base_length, current_length=1, space_padding=9): if current_length > base_length: return # Base case: stop the recursion when we've reached the full base length of the triangle. # Calculate the number of spaces to print before the '*' for the current line. spaces = ' ' * (space_padding + (base_length - current_length) // 2) stars = '*' * current_length # Output the line. print(spaces + stars) # Recursive call with the next line having current_length increased by 2 to add two more '*'. draw_triangle(base_length, current_length + 2, space_padding) # Example function call: base_length = int(input()) draw_triangle(base_length) - User
Latest submission - 11:03 PM PST on 12/09/24
Total score: 2 / 10
Open submission's code 1: Compare output 0 / 2 Compare output
Input 3 Your Output Expected output
* *** * ***Output is nearly correct, but whitespace differs. See highlights above. Special character legend 2: Compare output 0 / 2 Compare output
Input 19 Your Output Expected output
* *** ***** ******* ********* ***********
* *** ***** ******* ********* ***********
Output is nearly correct, but whitespace differs. See highlights above. Special character legend 3: Compare output 2 / 2 Compare output
Input 1 Your Output Expected output * * 4: Compare output 0 / 2 Compare output
Input 9 Your Output Expected output
* *** ***** ******* ********* * *** ***** ******* ********* - Assistant