[issue34332] Suggestion for a new loop type

2018-08-04 Thread Stéphane Wirtel

Stéphane Wirtel  added the comment:

You can also use a context manager and the `with` statement.

--
nosy: +matrixise

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34332] Suggestion for a new loop type

2018-08-03 Thread Zachary Ware


Zachary Ware  added the comment:

Ideas like this are better sent to the python-id...@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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34332] Suggestion for a new loop type

2018-08-03 Thread Happy Fakeboulder


New submission from Happy Fakeboulder :

A "while-except" loop, in which it acts like a do-while loop from other 
languages, except the condition is "did an exception occur during the execution 
of the iteration". One could specify a type of exception at the top, similar to 
an except block header. (sorry, I'm an idiot)
Example (the keyword might be different):

# asks for a number until the user gives one properly, then
# calculates the square of it.
whexc ValueError:
num = int(input("Enter a number: "))
print("The square of your number is " + str(num * num)) # assuming whexc works, 
this will not cause an error

--
messages: 323065
nosy: Happy Fakeboulder
priority: normal
severity: normal
status: open
title: Suggestion for a new loop type
type: enhancement
versions: Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com