Reputation: 232
I'm having a bit of an issue. I'm trying to make this program output a right triangle based on the height and symbol that is specified by the user, but whenever I enter the symbol and height desired the program will output the correct shape, but upside-down. I've been having some difficulties getting a firm grasp of loops and through trial and error, this is the best I could come up with thus far. Could someone help a brother out, please. Thank yous in advance.
triangle_char = input('Enter a character:\n')
triangle_height = int(input('Enter triangle height:\n'))
print('')
for i in range (len(triangle_char)):
for j in range (triangle_height):
print((triangle_char) * triangle_height )
triangle_height -= 1
This code will return this output when character is '*' and height is 5:
*****
****
***
**
*
The expected output when those values are entered should be:
*
**
***
****
*****
Upvotes: 0
Views: 27199
Reputation: 333
Here it is as a script. I assumed that multiple input characters meant multiple output triangles. Also, that height:0 means zero lines in each triangle.
Tricky thing I learned today is that int("20.0")
doesn't convert to 20; it raises an exception. The code works around that by converting to float first.
#!/usr/bin/python3
def triangles(characters, height):
# We could call int(height) here except that int("20.0") for example raises
# an error even though there is a pretty clear integer value. To get around
# that, attempt to convert to float first.
try:
lines = float(height)
except ValueError:
# If we raise here, the error is like: cannot convert to float. That's
# confusing, so we let it go.
lines = height
# If the value can't be converted to int, this raises an error like: cannot
# convert to int. If it had already converted to float, this rounds down.
lines = int(lines)
for character in characters:
# Loop will execute no times if lines==0, once if lines==1 and so on.
for line in range(1, lines + 1):
print(str(character) * line)
print("")
if __name__ == '__main__':
try:
triangles(input("Enter characters: "), input("Enter height: "))
except ValueError as error:
print("Couldn't print triangles:", error)
Edit: Added sample output.
$ ./triangles.py
Enter characters: jk
Enter height: 8
j
jj
jjj
jjjj
jjjjj
jjjjjj
jjjjjjj
jjjjjjjj
k
kk
kkk
kkkk
kkkkk
kkkkkk
kkkkkkk
kkkkkkkk
$ ./triangles.py
Enter characters: .
Enter height: 3.0
.
..
...
$ ./triangles.py
Enter characters: f
Enter height: 3.7
f
ff
fff
$ ./triangles.py
Enter characters: duff
Enter height: duff
Couldn't print triangles: invalid literal for int() with base 10: 'duff'
Upvotes: 0
Reputation: 41
You can also do like this :
triangle_char = input('Enter a character: ')
triangle_height = int(input('Enter triangle height: '))
print('')
j = 0;
while j <= triangle_height :
print triangle_char * j
j += 1
Upvotes: 0
Reputation: 1669
First your loops are both off; you are assigning values to i
and j
and not using them.
Second, the first loop is useless. If you input 3 characters it will repeat the block 3 times, but the variable triangle_height
is decreased to 0 at the first pass, so nothing will be printed at the next iteration.
Just remove this line
Third: You say that you need the triangle reversed, so, instead of decreasing triangle_height
, use the value you assing to j
in the for loop and forget about decreasing the variable.
Since range starts counting from 0 you need to add 1 to it in the print statement:
triangle_char = input('Enter a character: ')
triangle_height = int(input('Enter triangle height: '))
print('')
for j in range (triangle_height):
print((triangle_char) * (j + 1))
I have also replaced /n
with a space in the input() methods because it looked pretty bad.
Upvotes: 2