[issue34332] Suggestion for a new loop type

Zachary Ware report at bugs.python.org
Fri Aug 3 15:52:12 EDT 2018


Zachary Ware <zachary.ware at gmail.com> added the comment:

Ideas like this are better sent to the python-ideas at python.org mailing list for discussion rather than immediately opening an issue.  Note though that I think this has a low chance of acceptance; there's a very high bar to clear to add syntax, even higher for new keywords, and this doesn't seem to buy much over:

   while True:
       try:
           num = int(input("Enter a number: "))
       except ValueError:
           print("I said a *number*")
       else:
           break
   print("The square of your number is", num**2)

Especially considering that this version allows you to handle the error however you want, and handle different errors differently, rather than just silently restarting the loop no matter the error.

Also, this will be valid in 3.8 and achieve exactly what you're asking for:

   while not (ans := input("Enter a number: ")).isnumeric():
       pass
   print("The square of your number is", int(ans) ** 2)


Do feel free to send your idea to python-ideas anyway, I'm just one opinion :)

----------
nosy: +zach.ware
resolution:  -> rejected
stage:  -> resolved
status: open -> closed
versions:  -Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34332>
_______________________________________


More information about the Python-bugs-list mailing list