This program will output a right triangle based on user specified height triangle_height and symbol triangle_char.
(1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangle_char character. (1 pt)
(2) Modify the program to use a loop to output a right triangle of height triangle_height. The first line will have one user-specified character, such as % or *. Each subsequent line will have one additional user-specified character until the number in the triangle's base reaches triangle_height. Output a space after each user-specified character, including a line's last user-specified character. (2 pts)
triangle_char = input('Enter a character:\n')
triangle_height = int(input('Enter triangle height:\n'))
print()
for i in range(1, triangle_height +1):
for j in range(1, i+1):
print('%c' %triangle_char, end=' ')
print()
QUESTION:
I did the coding, but I had a help on it.
1. I used j for second integer, but can I use any other letter than j, and why is it j?
2. '%c' what does that mean? does that mean it can print another character other than %?
1. Yes, you can use any other letter than j. j is just a variable name.
I have used temp variable insted of j and it returns the same output:
2. %c is used to print the character. That doesn't means it print character other than %. You can provide '%' character as triangle character and it prints the result as below.
In python you don't have to provide %c to print the character.You can use below code. that will print the same output as your code.
triangle_char = input('Enter a character:\n')
triangle_height = int(input('Enter triangle height:\n'))
print()
for i in range(1, triangle_height +1):
for j in range(1, i+1):
print(triangle_char, end=' ')
print()
Get Answers For Free
Most questions answered within 1 hours.