'I want to know a form of a "Repeat until" block in scratch in python? [duplicate]
I need some help, you know those "repeat until" blocks in scratch? Well I need to know how to do that in python. What I want to do is this, with a repeat block.
retry = True
if password = "password1234":
retry = False
else:
pass
Solution 1:[1]
The following snippet checks if the password is "password1234". If it is correct, it changes the flag to False and hence the loop terminates. Otherwise, it will do further processing (e.g., ask the user for a new input).
retry = True
password = ""
while (retry):
# Check if the password equals to a specific password.
if (password == "password1234"):
retry = False
else:
# Do further processing here.
# Example: ask the user for the password
password = input("Enter a password: ")
Solution 2:[2]
Program will be asking you for password until you type password1234
password = input("Enter you password")
while password!="password1234":
password = input("Enter you password")
Solution 3:[3]
The above solutions are also correct. But if you want to keep your style, try this:
def get_password():
retry = True
password = input("Password: ")
if retry:
if password == "password1234":
retry = False
else:
return get_password()
get_password()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hossam Magdy Balaha |
| Solution 2 | Worldmaster |
| Solution 3 | goku |
