[Tutor] Condition for variable which doesn’t exist yet

Julius Hamilton juliushamilton100 at gmail.com
Sat Oct 2 17:27:34 EDT 2021


Hey,

I’d like to make a while-loop stop on a condition related to an equivalence
statement on a variable (while L != “”). However, I’d prefer to not declare
this variable before the while loop, for the sake of elegance.

Is there any syntax or idea that comes to mind that would allow me to say “
while L!=“” “ and for the program to understand, “L doesn’t exist yet so
this is fine, the only issue is if it exists and it’s a blank string”?

Another idea I have is just using a different loop type. Maybe a syntax
like:

do:
  l = input()
  if l == “” break
  g.append(l)

What would be the Python for this?

But I can picture a more elegant way I’d love to see, something like this:

do (if l == “” break):
  g.append(l = input())

This hopefully says to check at all times if l is ever “”. If it ever
happens, break the loop. Meanwhile, the input is getting passed to the
variable l, and then appended straight to the list g, in one line of code.

Is this possible in Python or a different language?

Thank you very much,
Julius


More information about the Tutor mailing list