2014-11-22 4 views
0

문제 :

저는 컴퓨터가 생각하는 숫자를 추측하여 비단뱀 게임을 작성했습니다. 나는 당신이 점수를 얻는 정답을 얻었을 때 얼마나 많은 삶을 남겼는지에 따라 점수 체계를 추가했습니다. 또한 다시 게임 기능이 있으며 내 생각은 프로그램을 연 후에 다시 점수를 입력하라는 메시지가 나타나면 n을 누를 때 인쇄가 종료되기 전에 인쇄된다는 것입니다. 그러나 내 코드에는 버그가 있으며 점수를 인쇄 할 때 그림에 표시된 것처럼 마지막 게임에서 점수 만 얻으면 그림에 표시된 것과 같이 끝에 10 점이 있어야하지만 0이 인쇄됩니다. 몇 시간 동안 붙어있어. 내 코드는 아래에 포함되어 있습니다.Python 점수가 누적되지 않았습니다.

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() 

답변

0

: 또한 속기 연산자를 사용

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

과 같다 :

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

을 그 있었어야 생각 표현이 줄어들 것이다. 이것을 할 가능성이있다. 'score + = 5' –

+0

정말 고마워요! 나는 이것을 몇 시간 동안 꼼짝 않고 바라 보았다! – person101

+0

@DannyStaple 앞으로이 문제를 피하기 위해 이것을 사용할 것입니다. 고맙습니다 – person101

관련 문제