Python得分不累积

时间:2014-11-22 16:48:15

标签: python python-3.4

问题:

我编写了一个基于猜测计算机正在考虑的数字的python游戏。我已经添加了一个评分系统,取决于你得到正确答案时你得到分数的生命。还有一个播放功能,我的想法是当你再次播放时按n,打开程序后你的分数会在退出之前打印出来。然而,我的代码中存在一个错误,当它打印出分数时,你只能从你上一场比赛中得到的分数显示,例如图片中显示的最后你应该得到10分,但它打印为0.请帮助有了这个,我被困了好几个小时。我的代码包含在下面。

http://i.stack.imgur.com/mEoy8.png

CODE:

#imports required modules
import random
#score set as global variable with the value 0
global score
score = 0
#lower number set as global and value of user input
global low
low = int(input('Lowest number: '))
#lower number set a global and value of user input
global up
up = int(input('Hightest number: '))

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

#main game code
def main():
    global low
    global up
    #generates number at random between the users two inputs
    comp_num = random.randint(low,up)
    #score set as global variable within function
    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')
            #1 live left player gets 5 points
            if lives == 1:
                score == score + 5
                print('\nYou scored 5 points!')
            #2 lives left player gets 10 points
            elif lives == 2:
                score = score + 10
                print('\nYou scored 10 points!')
            #3 lives left player gets 15 points
            elif lives == 3:
                score = score + 15
                print('\nYou scored 15 points!')
            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
        print('\nYou scored',score,'points!')
        #score printed before exit
        input('Press enter to exit')
        exit()

#calls main section of game
main()

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

1 个答案:

答案 0 :(得分:0)

您遇到类型错误:

  if lives == 1:
            score == score + 5

我想应该是:

  if lives == 1:
            score = score + 5