简体   繁体   中英

Adding scoring system to a number guessing game including play again

I have my game working fine now but I still want to take it further. I want to have a point scoring system included. I want the player to score 5 points if they have 1 live left when they win, 10 if they have 2 and 15 if they have all 3. But I don't want the score to be reset if they play again I just want it to say when the quit the game you have scored " " points. I have tried to do this in many different ways but I can seem to get it work it resets the score every time press y on the play again. I've included my base game code below please try and help. Any other recomendations for this game are very welcome.

**My apologies I don't know if I made this clear enough before. I don't want the score to be stored after the programs closed just until the player presses n when asked to play again **

#imports required modules
import random

#correct number variable created
num = 0

#generates number at random
comp_num = random.randint(1,10)

print('I\'m thinking of a number guess what it is...\n')

#main game code
def main():
    #generates number at random
    comp_num = random.randint(1,10)
    #set num as a global variable
    global num
    #lives created
    lives = 3
    while lives >= 1:
        #player guesses
        guess = int(input('Guess: '))
        if comp_num == guess:
            #if correct says well done
            print('\nWell Done! You guessed Correctly!\n')
            break
        elif comp_num >= guess:
            #if guess is too low tells player
            #one live taken for incorrect guess
            lives = lives -1
            print('\nToo low!\n')
            #player is told how many lives they have left
            print('You guessed incorrectly. You have',lives,'live(s) remaining.\n')
            if lives == 0:
                    #if player guesses incorrectly they get told the correct awnser
                    print('The number I was thinking of was...',comp_num,'!\n')
        elif comp_num <= guess:
            #if guess is too high tells player
            #one live taken for incorrect guess
            lives = lives -1
            print('\nToo high!\n')
            #player is told how many lives they have left
            print('You guessed incorrectly. You have',lives,'live(s) remaining.\n')
            if lives == 0:
                    #if player guesses incorrectly they get told the correct awnser
                    print('The number I was thinking of was...',comp_num,'!\n')

def end():
    #asks player if they want to play again
    play_again = input('Would you like to play again?[Y/N] ')
    while play_again.lower() == 'y':
        #if they do game resets and plays again
        if play_again.lower() == 'y':
            comp_num = random.randint(1,10)
            print('\nI\'m thinking of a number guess what it is...\n')
            main()
            play_again = input('Would you like to play again?[Y/N] ')
            if play_again.lower() == 'n':
                break
    if play_again.lower() == 'n':
        #if they don't game ends
        input('Ok, Press enter to exit')
        exit()

#calls main section of game
main()


#calls end of game to give option of playing again and reseting game
end()

Use this pattern:

def game():  # Play a single game.
  lives = 3
  ...
  return 5 * lives  # Return the score of the game.

def ask_again():
  print 'Play again?'
  answer = ...
  return answer == 'y'  # Return True iff the user wants to play again.

def main():
  score = game()
  while ask_again():
    score += game()
  print score

main()         

You should use global variable for storing the amount of scored points.

So I added code for adding points and printing the message:

#imports required modules
import random

#correct number variable created
num = 0

score = 0

#generates number at random
comp_num = random.randint(1,10)

print('I\'m thinking of a number guess what it is...\n')

#main game code
def main():
    #generates number at random
    comp_num = random.randint(1,10)

    #set num as a global variable
    global num

    global score

    #lives created
    lives = 3
    while lives >= 1:
        #player guesses
        guess = int(input('Guess: '))
        if comp_num == guess:
            #if correct says well done
            print('\nWell Done! You guessed Correctly!\n')

            # add score
            if lives == 3:
                score += 15
            elif lives == 2:
                score += 10
            elif lives == 1:
                score += 5

            break
        elif comp_num >= guess:
            #if guess is too low tells player
            #one live taken for incorrect guess
            lives -= 1
            print('\nToo low!\n')
            #player is told how many lives they have left
            print('You guessed incorrectly. You have',lives,'live(s) remaining.\n')
            if lives == 0:
                    #if player guesses incorrectly they get told the correct awnser
                    print('The number I was thinking of was...',comp_num,'!\n')
        elif comp_num <= guess:
            #if guess is too high tells player
            #one live taken for incorrect guess
            lives -= 1
            print('\nToo high!\n')
            #player is told how many lives they have left
            print('You guessed incorrectly. You have',lives,'live(s) remaining.\n')
            if lives == 0:
                    #if player guesses incorrectly they get told the correct awnser
                    print('The number I was thinking of was...',comp_num,'!\n')

def end():

    global score

    #asks player if they want to play again
    play_again = input('Would you like to play again?[Y/N] ')
    while play_again.lower() == 'y':
        #if they do game resets and plays again
        if play_again.lower() == 'y':
            comp_num = random.randint(1,10)
            print('\nI\'m thinking of a number guess what it is...\n')
            main()
            play_again = input('Would you like to play again?[Y/N] ')
            if play_again.lower() == 'n':
                break
    if play_again.lower() == 'n':
        #if they don't game ends

        print("You scored " + str(score) + " amazing points!")

        input('Ok, Press enter to exit')
        exit()

#calls main section of game
main()


#calls end of game to give option of playing again and reseting game
end()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM