import sys
from easygui import boolbox, choicebox
def reallyquit():
"Ask if the user wants to quit. If so, exit."
if __debug__:
print("really quit?")
answer = boolbox("Are you sure you want to Quit?")
if answer == 1:
sys.exit()
columns = ['adr1', 'adr2', 'spam', 'spam']
street = None
# ask for address until given or user decides to quit
while street is None:
street = choicebox("Street Address?", title, columns)
if street is None:
reallyquit()
In the main logic: Usually you would not test if a value is equal to None
, since it is a singleton (only one in the system, and all occurrences of None
are to the same logical None
). Thus is None
is a better test. I've also used a little more Pythonic code formatting (spaces after commas in a list, lowercase variable names), and a test order that doesn't duplicate the user interaction call.
An alternative form suggested by Eric would be something like:
while True:
street = choicebox("Street Address?", title, columns)
if street is not None:
break
reallyquit()
This has the virtue of not repeating the street is None
test, but is a bit longer. Your preference for which option seems clearer and more logical may vary.
For the preparations: Imports are taken out of functions, and choicebox
is imported where it was previously missing. The function is given
a defining comment string. The one statement per line standard is enforced. And the unnecessary return
removed.
This is still a fairly coarse program, but it's now "more Pythonic."
Not to be overwhelming, but the standard for Pythonic code appearance is PEP 8. There are tools such as pep8 you can install to check compliance. The deeper or "semantic" part of Pythonic code is sketched out in PEP 20, also known as "The Zen of Python." There is, sadly, no tool to judge compliance with those deeper principles. Experience is required.