The following code will print a triangle of stars.
How we can obtain the same result using while loop? And explain how it works?
Thanks in advance for any help?
l=int(input("Enter the limit:"))
for i in range(1,l+1):
for j in range(i):
Why don’t you first try to implement it yourself? You need to learn how to implement while loops at some point. Think about what the
range function does for you and then create the necessary variable(s) to get it to work. If you get stuck, post your code attempt and we will try to guide you to a solution.
FYI - The code above does not print a triangle of stars.
well to understand b we must firstly understand a right?
so let’s take a look what everything does in this code shall we.
l is like an x we use in maths
with the = we asign the value of int to it
int stands for integer.
Then we have the double paranthese which we general use to store things in we need like tentacles we can use the stuff we stored inside it anywhere
then the word input which speaks for itself it inputs something
then a string we can conclude it is because it has the “” which cary’s the word Enter the limit in it
now comes the loop the for indicatins it is a for loop. which indicates FOR a crtain of times. then the next condition which is i in range. a in range is a keyword again and indicates that the range of the number/alphabetical are counted then comes another set of parenthese which include the num of 1 the I which reverst back to the 1st line so where not reading I here we read what is assigned to I here. The next one is +1 which does mean each time the loop runs it adds a 1 to the value. we then end the line and go to the second
just like u would use an x and y in math so do we use a j here we got ovr the range part already. but we can also find the added (i) so the 3rd line is calling back to the second
then the print which prints the whole thing out between the parenthese we see it calls to the * followed which in or function will call to the first strong by a , which also can be replaced with the + then the end is equal to the string
if u want to use an while loop here u must use the while keyword and realize that a while loop runs while a certain condition is true
to achieve the same result all you would have to do is figurin out how many times u need ure loop torun and most likely replace the second line
hope it helps let me know if anything was unclear