Posts

Showing posts with the label While Loops

Python Exercise 5 --- "Astrologer's Stars"

 Python  Exercise 5 --- "Astrologer's Stars" In today’s exercise, what we have to do is, we have to print a pattern similar to that of  a  right-angle triangle , such as: * ** ** * ** ** Copy You have to follow certain instructions, which are as follows: You have to take an integer type variable, and the input of the variable will define the length of the triangle. You have to declare another Boolean variable. When the value of Boolean is 1 i.e. True, the pattern will be printed as shown above. But if the value of Boolean is 0 or false, then the triangle will be printed upside down . Solution: a = int ( input ( "Enter the number of lines you want to print \n " )) b = bool ( int ( input ( "Enter 0 or 1 \n " ))) def star (a , b): if b == True : c = 1 while c <= a: print (c * "*" ) c = c + 1 else : while a > 0 : print (a * "*" ) a = a - 1 star(a , ...

Python Tutorial 10 --- "While Loops In Python"

 Python Tutorial 10 --- "While Loops In Python" This PROGRAM or TUTORIAL about Python's  "While Loops In Python" DOC As we know now what loops are and why are they used, in this section, I will go directly towards the working and syntax of while loop along with its comparison with for loop and where to use it. The syntax for while loop is simple and very much like for loop. We have to use the keyword “while”, along with it we have to put a condition in parenthesis and after that, a colon is placed. The condition could be either true or false. Until the condition is true, the loop will keep on executing again and again. If we use a certain sort of condition in our while loop that, it never becomes false then the program will keep on running endlessly, until we stop it by force. So, this kind of mistake in our syntax is known as logical/human error. Let us understand the concept of the while loop and why do we use it in areas where the number of iterations are not...