First find what loop you need. If the number of iterations is known beforehand then for. If not known then while. Then find how to draw a random number. Test in the python interpreter. Then find how to use the input function. Test in tje python interpreter. Brick by brick you will build you way ;) Answer from kowkeeper on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do you loop until correct input is given?
r/learnpython on Reddit: How do you loop until correct input is given?
February 25, 2022 -

I'm doing an assignment that requires I make a code that creates random numbers in a loop until I give a specific input. I just don't know how to get the loop to run while an input is requested. PLZ HELP!

EDIT:

To clarify the assignment:

Write a program that enters a number of positive integers.

The program ends when the user gives in a negative number.

a) Before the program ends, the largest number is printed.

b) Before the program ends, the smallest number is printed.

c) Before the program ends, the sum of the even numbers and the sum of the odd numbers are printed.

(I can do the a,b,c part no problem. It's the Program that creates the numbers i have problems with)

I asked my teacher what he meant by "a number of positive integers". He stated that the program should create numbers until the negative number is given.

EDIT 2:

I have concluded that I'm an idiot. It is the user who inputs the numbers. Thank you all for the help and not giving up with my dense skull :) (great community๐Ÿ‘)

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 33399615 โ€บ how-loop-an-input-over-until-correct-answer-is-entered
python - How loop an input over until correct answer is entered - Stack Overflow
October 29, 2015 - Also you shoul use if nameInput == answer: because answer is not a list. ... You have the wrong order for the operands to in - it's if needle in haystack not if haystack in needle.
Discussions

loops - Is there a "do ... until" in Python? - Stack Overflow
Is there a do until x: ... in Python, or a nice way to implement such a looping construct? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to do while loop until my output has reached - Stack Overflow
I have a password verifier and I have made it that when there is less than 6 digits to change a variable called "invalid" to 1 and if it is is more than 16 digits to change a variable cal... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the most efficient way to keep a loop until a user input is right ?
In addition to the already provided examples, you could use the walrus operator: prompt = "Input directory here:\n" while not (paths := Path(input(prompt))).is_dir(): print(f"{paths} is not a valid directory...") Whether it's more pythonic or not depends on who you ask. Personally I like it, but it does have its downsides. More on reddit.com
๐ŸŒ r/learnpython
21
13
May 14, 2024
How to run python loop until correct input string is entered - Stack Overflow
I have to write a code that will execute the below while loop only if the user enters the term "Cyril". I am a real newbie, and I was only able to come up with the below solution which wo... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 27, 2020
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ while-loop
Python while Loop (With Examples)
If the condition is True, body of while loop is executed. The condition is evaluated again. This process continues until the condition is False.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ loops-in-python
Loops in Python - GeeksforGeeks
In Python, a while loop is used to execute a block of statements repeatedly until a given condition is satisfied.
Published ย  1 week ago
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - You also need to make sure that they terminate correctly. Normally, you choose to use a while loop when you need to repeat a series of actions until a given condition becomes false or while it remains true.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
Find elsewhere
Top answer
1 of 4
2

Initialize a variable to check if the conditions are met. The while loop will run until password is of right size

check = 1
while check == 1:
    input = input('Give your password')
    if 6 < len(input) < 16:
        check = 0
2 of 4
0

This is how I would do it, after reading your current code:

import re

print("""
    Your password needs to have the following:
    
    At least 1 lowercase character

    At least 1 uppercase character

    1 of these special characters ($#@)

    Has to have a minium of 6 characters and a maximum of 16 characters
""")

invalid = 1
while invalid:
    password = input("Please input a Password: ")
    if len(password)<6 or len(password)>16:
        print("Password length invalid, please try again")
    elif not any(x in password for x in {"$","#","@"}):
        print("Password must contain one of these special characters: $#@, please try again")
    elif not re.search(r"[A-Z]", password) or not re.search(r"[a-z]", password):
        print("Password must contain at least one capital and one lower case character, please try again")
    else:
        print("Password valid")
        invalid = 0

Result:

    Your password needs to have the following:

    At least 1 lowercase character

    At least 1 uppercase character

    1 of these special characters ($#@)

    Has to have a minium of 6 characters and a maximum of 16 characters

Please input a Password: hejhejhejhejhejhejhejhej
Password length invalid, please try again
Please input a Password: hej
Password length invalid, please try again
Please input a Password: hejhejhej
Password must contain one of these special characters: $#@, please try again
Please input a Password: hejhejhej#
Password must contain at least one capital and one lower case character, please try again
Please input a Password: Hejhejhej#
Password valid
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-construct-while-loops-in-python-3
How To Construct While Loops in Python 3 | DigitalOcean
August 20, 2021 - The code that is in a while block ... if statement, the program continues to execute code, but in a while loop, the program jumps back to the start of the while statement until the condition is False....
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-38568.html
Looping a question until correct feedback is given
October 30, 2022 - I'm writing my first python program as a number guessing game. I am trying to loop a question until a 'yes' or 'no' response is given but am having trouble figuring it out. ''' response = input('Do you want to try a guessing game?: [yes or no]') whi...
๐ŸŒ
Manifoldapp
cuny.manifoldapp.org โ€บ read โ€บ how-to-code-in-python-3 โ€บ section โ€บ a07cdae8-713a-4c9a-adb1-63cf5b0006b5
How To Construct While Loops | How To Code in Python 3 | Manifold @CUNY
The code that is in a while block ... if statement, the program continues to execute code, but in a while loop, the program jumps back to the start of the while statement until the condition is False....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-while-loop
Python While Loop - GeeksforGeeks
Pass is also used for empty control statements, functions, and classes. ... As discussed above, while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed.
Published ย  December 23, 2025
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ while-loops-in-python-while-true-loop-statement-example
While Loops in Python โ€“ While True Loop Statement Example
July 19, 2022 - If the condition evaluates to True, then the loop will run the code within the loop's body and continue to run the code while the condition remains True. It will keep executing the desired set of code statements until that condition is no longer ...
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-while-loop
How to Write and Use Python While Loops | Coursera
February 24, 2023 - First, the Python interpreter checks the test expression or, while condition. If the test expression evaluates to true, the body of the while loop will be entered. This process repeats until the test expression evaluates to false.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Help with a while loop - Python Help - Discussions on Python.org
February 5, 2024 - I need help fixing up this while loop. When I place my answer I end up with the same answer โ€œThat is not A, B, C, D or E, try again:โ€ print("1. What is the smallest unit in life? -'2points'-") print("A: organs") print("B: cells") print("C: organism") print("D: tissues") print("E: organ system") print("---------------") organs = ("A") cells = ("B") organisms = ("C") tissues = ("D") organ_system = ("E") answers = (organs, cells, organisms, tissues, organ_system) quest1s = input("?: ") while quest...
๐ŸŒ
Devcamp
bottega.devcamp.com โ€บ full-stack-development-javascript-python โ€บ guide โ€บ overview-while-loops-python
Overview of While Loops in Python
No, 55 is not the answer. Please try again. So notice the behavior here because we have a while loop. I could go on for this for hours and I could guess a thousand different numbers and it's going to keep on going through because we have a while loop here. It will keep the program running until it reaches that sentinel value until it returns false. Now if I type in the number 42. It says you correctly guessed it and then notice the while loop ended.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ using a while loop to ask the user (y/n) ?
r/learnpython on Reddit: Using a while loop to ask the user (Y/N) ?
October 31, 2023 -

So I have a program that calculates compound interest, and now I need to add a question at the end of my program that asks the user if they would like to run the program again, if they answer with Y or y the program will restart to the beginning, if they answer โ€œNโ€ or โ€œnโ€ the program is over. And if they answer with anything other than y or n it should repeat the question until the user finally enters y or n

What I have so far:

I have my entire program in a while loop and was able to get it to reset while Var == Y or y. But I canโ€™t get it to repeat the question if the user answers with letโ€™s say โ€œqโ€ or โ€œ618โ€ or โ€œLโ€. Does anyone know how to do something like this, please let me knowโ€ฆ thank you

๐ŸŒ
Study.com
study.com โ€บ courses โ€บ computer science courses โ€บ computer science 113: programming in python
While Loops in Python | Definition, Syntax & Examples - Lesson | Study.com
July 24, 2024 - A while loop is used to repeatedly execute the indented block of code as long as the True-False condition following the word 'while' evaluates to True. It is ideal for situations where the total number of iterations necessary cannot be defined ...