[Tutor] Two Problems (databases, and branching)

dman dman@dman.ddts.net
Sat, 27 Apr 2002 14:18:41 -0500


--KsGdsel6WgEHnImy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat, Apr 27, 2002 at 01:42:52PM -0300, Spotacus wrote:
| \\ What are your requirements for this database?  There are many choices
| out there.
| =20
| I don't have much in the way of requirements. Just, something where part
| of it can't be changed, that stores the info about the items the
| merchant sells (name, buying price, selling price, etc), and then
| another part that stores everything that the character is carrying. I'm
| pretty sure that's how it'd work.

I think that pickle is probably best suited for your task.  See the
documentation for the pickle module for the details, but basically
there are two methods.  One takes an object and stuffs it in a file,
the other takes a file and gives back an object.

You'll want to make Merchant and Player classes that you can
instantiate so that you have some cohesive objects to work with rather
than lots of globals to try and manage.

| \\ Structured <file:///\\Structured>  languages do it this way:
| \\ # decide which way to branch
| \\ if cond :
| \\     mwin("Text in a message window")
| \\ else :
| \\     # :here
| \\     print "the else block"
| =20
| I could do that, but I was thinking of something along the lines of
| skipping the whole program over. An example could be something that
| needed a password? Not anything high calibur, but simple, like:
| =20
| password =3D raw_input("What's the password? ")
| if password !=3D "whatever"
|     branch(":end")
| #---entire program goes here
| :end
| =20
| That's the kind of thing I was thinking, possibly. If I used an if
| statement to that effect, wouldn't I have to put the entire program
| inside the if statement?

Yes and no.  The _source code_ for the entire program doesn't need to
go inside the if-block, but a function call can.  For example :


def my_apps_main() :
    # beginning of the entire program goes here
    # the rest of the program should be in other functions
   =20
# give the user 3 tries
for _ in range( 3 ) :
    password =3D raw_input("What's the password? ")
    if password =3D=3D "whatever"
        my_apps_main()
        break
    else :
        print "Wrong password, try again"
   =20

The addition of the loop gives the user 3 chanced to enter the right
password.  If they get it right, control will move to the "main"
function, then the loop will terminate and the app will terminate.
Otherwise the error message will be printed and the program will try
again.  If the limit is reached then the program terminates (without
reporting it).

The "branch" is done by calling a function.  In that manner the whole
program is "in" the if-block, but you don't write all the source code
there.

| \\ How about posting that 80-odd line script and letting someone with
| \\ some spare cycles refactor it to give you a concrete example of
| \\ how it works?
| =20
| Um, okay. The program's just a simple calculator that I was doing to
| test out some things,

Cool.  That's a good way to start.

| and it ended up being a gigantic mess of whiles and ifs.

Without structure that can happen easily :-).

I'll post a response to that in another message.

WHile you're at it, check out Alan Gauld's tutorial.  It's called "How
to Program" or something like that.  Google knows how to find it :-).

-D

--=20

"Don't use C;  In my opinion,  C is a library programming language
 not an app programming language."  - Owen Taylor (GTK+ developer)
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--KsGdsel6WgEHnImy
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjzK+ZEACgkQO8l8XBKTpRRUbQCguBkb5Agd34Y47Q+LcZN3GVie
sqoAoLiaqdOa3b+CW7l40hsoQwu11mbt
=QRP/
-----END PGP SIGNATURE-----

--KsGdsel6WgEHnImy--