[Python-ideas] A suggestion for a do...while loop
Rob Cliffe
rob.cliffe at btinternet.com
Sun Jun 25 20:25:20 EDT 2017
On 25/06/2017 12:58, Markus Meskanen wrote:
> I'm a huge fan of the do...while loop in other languages, and it would
> often be useful in Python too, when doing stuff like:
>
> while True:
> password = input()
> if password == ...:
> break
>
> [...]I suggest [...]
>
> do:
> password = input('Password: ')
> until password == secret_password
>
> # This line only gets printed if until failed
> print('Invalid password, try again!')
>
>
I don't see any significant advantage in providing an extra Way To Do
It. Granted, the "while True" idiom is an idiosyncrasy, but it is
frequently used and IMHO intuitive and easy to get used to. Your
suggestion doesn't even save a line of code, given that you can write:
while True:
password = input('Password:')
if password == secret_password: break
print('Invalid password, try again!')
Regards
Rob Cliffe
More information about the Python-ideas
mailing list