[Tutor] Python Script Problem. Simple sql problem & py problems.

w chun wescpy at gmail.com
Tue Dec 6 23:47:52 CET 2005


> I wrote my first script (ever) in Python.
>               :
> The second one is more python specific I guess. The programs prints
> the "Exiting" string on exit one time for every action I do. Which
> means that if I add 4 users, I'll see 4 time exiting printed. If I add
> 2 users and delete 1 user, I'll see 3 exiting prints and so on. Any
> idea why does this happen?


hi, and welcome to Python!  for your 1st script, it looks pretty good.
 as far as the 2nd problem is concerned, it's all in the cheers()
function.

first of all, your code won't compile due to indentation issues.  i
assume that your if stmts are lined up like this:

    if choc == '1':
         sql_adduser(sql_host, sql_user, sql_pass, sql_dtbs, groupid)
         cheers()

... rather than how they're formatted online.  more importantly, it
appears that you are calling cheers() in a recursive way, for
functionality that doesn't require it.  it is because you call
cheers() again and again recursively that lead you to seeing "Exiting"
for each user you enter (or for any action).  what you should do is to
put things in a while loop to do the desired repeat until the single
exit:

def cheers():
    while True:
        print "Type [1] to add a user"
        print "Type [2] to delete a user"
        print "Type [3] to list the dbase"
        print "Type [99] to install (default installation, see README)"
        print "Type anything else to exit"

        choc = raw_input("Choose [1, 2, 3]: ")
        if choc == '1':
             sql_adduser(sql_host, sql_user, sql_pass, sql_dtbs, groupid)
        elif choc == '2':
             sql_deluser(sql_host, sql_user, sql_pass, sql_dtbs)
        elif choc == '3':
             sql_listusers(sql_host, sql_user, sql_pass, sql_dtbs)
        elif choc == '99':
             sql_install(sql_host, sql_user, sql_pass, sql_dtbs)
        else:
             break
    print "Exiting"

cheers()

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2006,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list