Guessing game High score db help

This is the place for queries that don't fit in any of the other categories.

Guessing game High score db help

Postby giodakid » Thu May 09, 2013 1:44 am

hey guys i need some help with my guessing game. The main guessing function in my game works but I want to add a 'high-score' list preferably by using a database. Ideally, I want a username and a score to be written and saved to the database for users that score more than 10 pts. I hope to be able to then retrieve the list of high scores which would display each user and the respective score.

Code: Select all
print('What is your name?  ')
myName = raw_input()
import anydbm
db = anydbm.open('scores.db', 'c')

db[myName] = str(0)

def main_game(myName, score):
   
    import random
    number = random.randint(1, 13)
    guessesTaken = 0
   
    print ''
    print('Ok ' + myName + ', I am thinking of a number between 1 and 13.')
    print ''
       
    while guessesTaken < 3:
        guess = int(raw_input('Take a guess  '))
       
        guessesTaken += 1
           
        if guess < number:
            print('Your guess is too low.')
            print ''
           
        if guess > number:
            print('Your guess is too high.')
            print ''
           
        if guessesTaken < 3 and guess != number:
            cont = raw_input('Wanna guess again?  ')
           
            if cont[0].lower() == 'n' or guessesTaken > 2:
                break
           
        if guess == number:
            break
           
    if guess == number:
        print ''
        print ('Bingo!! My number was ' + str(number) + '.')
        print ''
        print('    Good job, ' + myName + '! You guessed my number in ' + str(guessesTaken) + ' guess(es)!')
       
       
        int(guessesTaken)
       
        if guessesTaken == 1:
            score += 3
        if guessesTaken == 2:
            score += 2
        if guessesTaken == 3:
            score += 1
       
        print('    You\'ve earned ' + str(score) + ' point(s) so far!')
        print ''
       
        db[myName] = str(score)
       
        return score

    if guess != number:
       
        print ''
        print('Sorry ' + myName + ', the number I was thinking of was... ')
        print str(number)
        print ''
        print('You\'re total score is: ' + str(score))
        print ''
       
        return score
       
        db[myName] = str(0)
       
while 0==0:
   
    score = main_game(myName,int(db[myName]))
   
    playagain = raw_input('Would you like to play again?  ')
   
   
       
    if playagain[0].lower() == 'n':
        print ''
        print ('Thanks for playing! You\'re final score was ' + str(score) + '. See you next time.')
   
   
        break
giodakid
 
Posts: 1
Joined: Thu May 09, 2013 12:47 am

Re: Guessing game High score db help

Postby metulburr » Thu May 09, 2013 1:55 am

Ideally, I want a username and a score to be written and saved to the database for users that score more than 10 pts.

I dont think anyone here is going to just write it out for you. First step would be to put your code aside, whatever database you choose, figure out how to read and write to it, search it, and them implement it into your program.

There are some things that bother me about your code as is:
put imports at the top

just use
Code: Select all
while True:
instead of while 0==0:

Code: Select all
 print('    Good job, ' + myName + '! You guessed my number in ' + str(guessesTaken) + ' guess(es)!')

use format to insert the values into the string
print(' Good job, {}'! You guessed my number in {} guess(es)!'.format(myName, guessesTaken))

also why did you choose anydbm?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1418
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests