[Tutor] Question about comparing values

Alan Gauld alan.gauld at btinternet.com
Mon May 23 01:57:45 CEST 2011


"Becky Mcquilling" <ladymcse2000 at gmail.com> wrote

> I'm doing a tutorial and was given a problem, using shelve.

Remember that a shelve is basically a dictionary stored
in a file rather than in memory. So treat it like you would
a dictionary.

> Here is what I have so far:
>
> import shelve
>
> def user_scores():
>  user_name = input ("What is the user name?").rstrip()
>  scores = int(input("What is the users score, enter 0 if done"))
>  shelf = shelve.open('scores.py', writeback=True)
>  score = [scores]

I've no idea what you think this is doing?
Its actually creating a single valued list and assigning that to 
score.
Is that what you intended? If so why?

>  user = {user_name:score}
>  shelf['user'] = user

This is storing a single dictionary under the key 'user'.
Wouldn't it be easier to store the score under
the key user_name? That way you could store
more than one value. As it is you can only ever
have a single entry for 'user'  in your shelf.


>  shelf.close()
>  while scores > 0:
>    scores = int (input("What is the next score"))
>    shelf = shelve.open('scores.py', writeback=True)
>    score.append(scores)

OK, so now you append more scores to the liist,
but I still don't get why you need a list?

>    shelf['score'] = score

And now you add a new entry under 'score' which is the
list of scores.

>    shelf.sync()
>    shelf.close()

So at the end you haven't read anything from your shelf
but you have written two entries, one with a dictionary
of name and list. The other with a list of scores where
the first value happens to be the same as the one stored
under 'user'

And in memory you still have that raw data, at least
until you exit the function...

> What I'm not sure of is how to compare the values
> of score, to find and print the one that is highest.

I think you have far bigger problems to solve than that!

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list