DS160123 doubt about to run "while loop"

date 2/2/2023
day by day sessions enhance curiosity and afford to learn new things.
today we discuss following topics:

  1. while loop

break keyword
continue keyword
very good session but for non IT field its little bit difficult. trying hard.
print numbers less than 50.
i made a code like
age = 50
while(count < 25):
round = (count + 1)
but its not run

1 Like

You have to practice a lot then only it will be easy for you .

(please read whole reply, I tried to explain the code and thought process behind it :slightly_smiling_face:)
code should be like this

count = 0                         ''' count is initialized'''
while(count < 50):           ''' condition should be '<50' as we want every no. less than 50'''  
   count = count+1           ''' count is used to store no. from 1 to 49 and also to reach the 
                                           end condition of count = 50 '''
   print(count)                   '''we are printing the value of count'''

few suggestions regarding your code:

  1. you declared variable ‘age’ but not used it, it’s a bad practice.

  2. verify the condition with question requirements.

  3. first declare the variable before using in while loop

  4. before declaring a variable, think what is it’s use (it will be hard for non IT background, but
    after some time you will not face point 1 :slightly_smiling_face:)

    for example ,in the question we are asked to print no. 1 to 49, so we need
    1.one variable to store the no.
    2.one variable for the condition check.
    but we can do both the tasks using one variable, so we used only one.

  5. you did not initialize ‘round’ but used it. (If you meant to use count here, then it will be a
    printing mistake/autocorrect.)
    If you meant to use it, it’s job should be one of two from point 4.
    from what I can see, you meant it for condition check so your code should be like this:

count = 1                         # initializing the variable for first print statement
round = 1                         # initializing for first condition check
while( round < 50):           # declaring the condition
     print(count)                   # printing for present loop before incrementing for next loop
     round = round +1         # incrementing for next loop
     count = count  +1         # incrementing for next print

Here, we are using ‘round’ for condition check and ‘count’ for printing the numbers.


Nice explanation, keep the pace up :relaxed:.
Keep Practicing, Keep Learning.