12

WHILE LOOP

by

The Education Machine

[support@theeducationmachine.com]


A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is TRUE.

Syntax while expression: statement(s)

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.

For loop auto picks the next value but we would need to do the increment manually in while loop

  x=5
  while x<10:
      print(x)
      x+=1

Output:
5
6
7
8
9

Check in how many years my investment of 1L will be doubled to 2L or higher.

  interest=.09
  principal=100000
  year=0
  while principal <= 200000:
      principal+=principal*interest                        #principal = principal + (principal * interest)
      year+=1
      print(f"Year: {year},Principal: {principal}")

Output:
Year: 1,Principal: 109000.0
Year: 2,Principal: 118810.0
Year: 3,Principal: 129502.9
Year: 4,Principal: 141158.161
Year: 5,Principal: 153862.39549
Year: 6,Principal: 167710.0110841
Year: 7,Principal: 182803.912081669
Year: 8,Principal: 199256.2641690192
Year: 9,Principal: 217189.32794423093

Take a number from user, till it matches the winning number of 7, keep prompting the user for inputs.

  num=7
  while True:
      user_num=int(input("Input your number"))
      if user_num == num:
          print("Congrats your guess is right")
          break
      else:
          print("please input again")

Output:
Input your number10
please input again
Input your number11
please input again
Input your number7
Congrats your guess is right

Take a number from user, till it matches the winning number of 7, keep prompting the user for inputs.

  num=7
  while True:
      user_num=int(input("Input your number"))
      if user_num == num:
          print("Congrats your guess is right")
          else:
          print("please input again")

Output:
Input your number10
please input again
Input your number8
please input again
Input your number7
Congrats your guess is right
Input your number9
please input again

reserved_keywords

Another way to write above program.

  num=7
  while 'a' == 'a':
      user_num=int(input("Input your number: "))
      if user_num == num:
          print("Congrats your guess is right")
          break
      else:
          print("please input again")

Output:
Input your number: 12
please input again
Input your number: 13
please input again
Input your number: 7
Congrats your guess is right

Take 3 numbers from user, when the count of num variable becomes greater than length of num, which is 2.Then 1st number will get raise to the power of 2nd number and whatever the outcome will be, the outcome will get raise to the power of 3rd number.

  num = input("Input any 3 digit number: ")
  i = 1
  final = int(num[0])
  while i < len(num):
      final = final ** int(num[i])                      #print(i, final, num[i])
      i += 1
  print(final)

Output:
Input any 3 digit number: 234
4096

Take 3 numbers from user and check their datatype.

  num = input("Input any 3 digit number: ")
  print(num,type(num))

Output:
Input any 3 digit number: 234
234 <class 'str'>

Take 2 numbers from user, begin and end. Sum all numbers in this range.

  num1, num2 = input("share 2 numbers above 1, separated by comma").split(",")
  num1 = int(num1)
  num2 = int(num2)
  final = 0
  while num1 <= num2:
      final += num1                                   #final = final + num
      num1 += 1
  print(final)

Output:
share 2 numbers above 1, separated by comma1,6
21