I making a program in which a single line of input containing an integer, print a triangle of asterisks of that size. But my program is not working like this. So can you please have a look on my code and can determine my mistake please. Thanks
line = int(raw_input("Number of lines: "))
a = line * '*'
print a
This is the output I want to produce:
Number of lines: 5
*
**
***
****
*****
Since this is self-education rather than actual classwork, it’s best explained with code and a detailed explanation of what you were doing wrong.
You’re mostly there, in that you have the input and the means by which you can output a certain number of asterisks. Your problem is that you’re only printing one line with the maximum number of asterisks.
You need to loop from 1 up to that number, each time outputting that same kind of line with an increasing length, something like:
With this code (entering 5), you basically loop with the variable
icontaining in sequence{1, 2, 3, 4, 5}(rangeis inclusive at the low end and exclusive at the high end, hence theline + 1).Within that loop, you output that number of asterisks, giving you your triangle shape: