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๐)
loops - Is there a "do ... until" in Python? - Stack Overflow
python - How to do while loop until my output has reached - Stack Overflow
What is the most efficient way to keep a loop until a user input is right ?
How to run python loop until correct input string is entered - Stack Overflow
Videos
There is no do-while loop in Python.
This is a similar construct, taken from the link above.
while True:
do_something()
if condition():
break
I prefer to use a looping variable, as it tends to read a bit nicer than just "while 1:", and no ugly-looking break statement:
finished = False
while not finished:
... do something...
finished = evaluate_end_condition()
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
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
Hi all ! Python hobbist hover here, was asking to myself if there is a pithonic way of keeping a loop until a user input is right, for example this is my piece of code ( which works ):
i = False
while i is not True:
paths = Path(input("Input directory here:\n"))
if paths.is_dir():
i = True
else:
print(paths, " is not a valid directory...\n")Is there a better way to do this or more readable ? Thanks in advance
Change beginning of code to:
while True:
name_question = input("You are the President of RSA, what is your name?: ")
if name_question == "Cyril":
name = True
break
else:
print("\nThat is incorrect, please try again.\n")
You can try this even if I don't understand if your question is easy or if I am an idiot:
name_question = input("You are the President of RSA, what is your name?: ")
while name_question != "Cyril":
name_question = input("You are the President of RSA, what is your name?: ")
...
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