If statement in while loop python

So I am still in the process of learning Python and I am having difficultly with while loops. I have a sample of code below that includes while loop and if and else statements. What I want it to do is print 'Less than 2' and 'Greater than 4' which it does, but it keeps running. It does not print it just once each which is what I would want it to do. Any help would be greatly appreciated!

counter = 1
while [counter < 5]:
    count = counter
    if count < 2:
        counter = counter + 1
    else:
        print['Less than 2']
    if count > 4:
        counter = counter + 1
    else:
        print['Greater than 4']
    counter = counter + 1 

asked Apr 25, 2016 at 14:12

A.LeBrunA.LeBrun

1511 gold badge1 silver badge11 bronze badges

3

counter = 1 
while [counter  4:
        print["Greater than 4"]
    counter += 1

This will do what you want [if less than 2, print this etc.]

answered Apr 25, 2016 at 14:24

trolley813trolley813

7997 silver badges15 bronze badges

I'm assuming you want to say Less than 2 or Greater than 4 while incrementing from 1 to 4:

counter = 1
while [counter < 5]:
    if counter < 2:
        print['Less than 2']
    elif counter > 4:
        print['Greater than 4']
    else:
        print['Something else'] # You can use 'pass' if you don't want to print anything here
    counter += 1

The program will never display Greater than 4 because your while condition is counter < 5.

answered Apr 25, 2016 at 14:22

Scratch'N'PurrScratch'N'Purr

9,0912 gold badges31 silver badges47 bronze badges

  1. Declare a variable called username1 and assign a value to it
  2. Declare a variable called password1 and assign a value to it
  3. Declare a variable called isCorrect and assign to it the boolean value True
  4. Create a program that asks the user to enter his username [variable name: username2] and password [variable name: password2] and save them
  5. The program will check if the entered username [username2] and password[password2] are the same defined in username1 and password1
  6. if Yes print HAPPY LOGIN
  7. if not ask again the user to enter new values and check again

answered Nov 19, 2020 at 14:04

1

What a while-loop says is if True, keep doing till False. If you watch automate the boring stuff- While Loops on YouTube it should give you a understanding of how a while loop can be used and why a if-statement can be best in other cases..

password = ''

while password != 'your password':
    password = input['Please enter your password... ']
print['Thank you']
  1. Variable password is set as a blank string

  2. password is not an input equal to string "your password", which makes the while expression True, while true repeat. if password does equal 'your password' expression is false, exit loop.

  3. While password equals anything except 'your password', expression is True, repeat loop till False.

  4. If loop becomes False, print end of line, 'Thank you', end program.

answered May 16 at 3:40

Loops are used in programming to repeat a specific block of code. In this article, you will learn to create a while loop in Python.

Video: Python while Loop

What is while loop in Python?

The while loop in Python is used to iterate over a block of code as long as the test expression [condition] is true.

We generally use this loop when we don't know the number of times to iterate beforehand.

Syntax of while Loop in Python

while test_expression:
    Body of while

In the while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates to True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False.

In Python, the body of the while loop is determined through indentation.

The body starts with indentation and the first unindented line marks the end.

Python interprets any non-zero value as True. None and 0 are interpreted as False.

Flowchart of while Loop

Flowchart for while loop in Python

Example: Python while Loop

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int[input["Enter n: "]]

n = 10

# initialize sum and counter
sum = 0
i = 1

while i 

Chủ Đề