Hey, i appreciate anybody who use up some of their own time to help me. I'n a newbie to python and i have a couple problems.

First, the score NEVER gets updated.

Second, i'm unable to select the same menu option twice in a row (eg select 'letterguess' first, complete it then unable to select it again), so i'd have to go back an forth between option 1 and option 2.

Thanks for any help

# opens the current highscore from a text file
def highscore():
    try:
        highscore = open("topscore.txt","r")
        topscore = int(highscore.read())
        print topscore
        highscore.close
        return topscore
    except:
        print "error"
        topscore = 0
        return topscore


# randomly chooses a word from a list
def getWord(wordList,Hintlist):
    randompick = random.randrange(0,len(wordList)-1)
    wordChoice = random.choice(wordList)
    print "The length of the word is ", len(wordChoice)
    print Hintlist[randompick]
    return wordChoice


# allows the user to guess the letters in a word
def letterguess(wordList,Hintlist,score):
    score == letterguess
    wordChoice = getWord(wordList,Hintlist)
    print "You have 6 letter guesses"
    global guessnum
    while guessnum!=6:
        letter = raw_input("Please choose a letter")
        guessnum = guessnum + 1
        if letter in wordChoice:
            print "The letter you guessed is in the word"
        else:
            print "Sorry, the letter you guessed is not in the word"
    wordGuess(wordChoice,score)
    return score


# this function allows the user 3 guesses to guess the whole word
def wordGuess(wordChoice,score):
    score == wordGuess
    while wholeguess!=3:
        global wholeguess
        userguess = raw_input("Try to guess the full word")
        if userguess == wordChoice:
            print 'Congratulations, you guessed the correct word'
            score = score + 1
            return score
        else:
            print "Sorry, you didn't guess the correct word"
            wholeguess = wholeguess + 1
    return score


# jumbles up a word
def wordJumble(wordList,Hintlist,score):
    score == wordJumble
    wordChoice = getWord(wordList,Hintlist)
    high = len(wordChoice)
    low = -len(wordChoice)
    for i in range(10):
        position = random.randrange(low,high)
        print "word[", position, "]\t", wordChoice[position]
    wordGuess(wordChoice,score)
    return score



# checks the users score
def checkscore(player,score):
    print player, "you have scored", score, "points"


# replaces the topscore with the new topscore
def replacement(topscore,thisplayer):
    if thisplayer > topscore:
        print "You are now the top scorer"
        text_file = open("topscore.txt","w")
        text_file.close()
    else:
        print "You have not topped the score this time"


# this is the options menu
def menu():
        print """ Menu
              1. Guess the word
              2. Word Jumble
              3. Check your score
              4. Exit """
        answer = raw_input("Please make a selection")
        return answer





import random

thisplayer = 0
wholeguess = 0
guessnum = 0
score = 0
answer = "0"


wordList = ['albatross','donkey','spaghetti','geranium','binary']
Hintlist = ['seabird','slow animal','italian meal','element','0100110']


topscore = highscore()

player = raw_input("Please enter your name")
print "Hi", player
while answer!="4":
    answer=menu()
    if answer == "1": letterguess(wordList,Hintlist,score)
    if answer == "2": wordJumble(wordList,Hintlist,score)
    if answer == "3": checkscore(player,score)
    if answer == "4":
        thisplayer = score
        replacement(topscore,thisplayer)
        break
    if answer not in ('1','2','3','4'):
        print "error message"

Recommended Answers

All 5 Replies

score == wordJumble

This does nothing

You should use if ... elif ... else for if statement at last lines. Most stuff you are doing by yourself could be done by standard functions, like random.choice and random.sample.

score parameter given to functions is 0, so score is allways zero.

score = 0
snipeti snip
thisplayer = score

Oops, forgot to get rid of those. They weren't supposed to be there.

Your global statements do not also make any kind of sense as those variables are only used locally and you are not using returned values from the functions for games.

You need to actually write the new high score out to file ...

def replacement(topscore,thisplayer):
    if thisplayer > topscore:
        print "You are now the top scorer"
        text_file = open("topscore.txt","w")
        text_file.write(str(thisplayer))
        text_file.close()
    else:
        print "You have not topped the score this time"

# testing ...
topscore = 100
thisplayer = 155
replacement(topscore,thisplayer)

You can include the checking of the correct answer in the menu() function this way ...

def menu():
    while True:
        print """Menu
                  1. Guess the word
                  2. Word Jumble
                  3. Check your score
                  4. Exit """
        answer = raw_input("Please make a selection: ")
        # return if the answer is 1 2 3 or 4
        # otherwise stay in the menu loop and ask again
        if answer in list('1234'):
            return answer


# testing ...
answer = menu()
print answer
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.