2014-11-21 70 views
0

所以我最近開始編程python,並且我有這個代碼的一個問題。當玩家在使用所有生命後得到的答案不正確時,應該打印出答案,但只有在第一次播放圖層時他們再次播放,如果發現錯誤,他們不會得到正確答案。當玩家得到正確的答案時,它也會這樣做。另外,當您使用再次播放功能時,電腦選擇的號碼保持不變。請儘量幫助我,但要記住我的理解手杖在Python的某些方面非常有限。我收錄了很多評論來幫助別人瞭解正在發生的事情。我已經包含了我的代碼以及我在shell中獲得的內容。Python猜測遊戲即使玩家獲得答案,也會打印正確的答案

代碼:

#imports required modules 
import random 
from time import sleep 

#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(): 
    #lives created 
    lives = 3 
    #correct number variable reset 
    num = 0 
    while lives >= 1: 
     #player guesses 
     guess = int(input('Guess: ')) 
     if comp_num == guess: 
      #if correct says well done 
      input('\nWell Done! You guessed Correctly!\n') 
      #player doesn't get told what the number is if there right 
      num = num +1 
      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') 
     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') 

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('\nOk, Press enter to exit') 
     exit() 

main() 

if num != 1: 
     #if player guesses incorrectly they get told the correct awnser 
     print('The number I was thinking of was...',comp_num,'!\n') 

end() 

SHELL:

I'm thinking of a number guess what it is... 

Guess: 5 

Well Done! You guessed Correctly! 

The number I was thinking of was... 5 ! 

Would you like to play again?[Y/N] y 

I'm thinking of a number guess what it is... 

Guess: 5 

Well Done! You guessed Correctly! 

Would you like to play again?[Y/N] y 

I'm thinking of a number guess what it is... 

Guess: 5 

Well Done! You guessed Correctly! 

Would you like to play again?[Y/N] y 

I'm thinking of a number guess what it is... 

Guess: 5 

Well Done! You guessed Correctly! 
+0

作爲一個側面說明:您可以使用雙引號字符串,所以你不需要用反斜槓轉義撇號。例如:'print(「我在想數字猜它是什麼...... \ n」)'。另外,你是否真的希望所有這些'\ n'到處都是,給你在每行輸出之間留出一條空白線? – abarnert 2014-11-22 00:05:09

+0

您在'main'之前初始化變量,然後重複調用'main'(使用相同的變量)。相反,在'main'裏初始化你的變量,這樣它們就不會被重用。 – khelwood 2014-11-22 00:15:11

+0

謝謝@abarnert會將其改爲雙引號 - 由於學校計算機系統較差,我需要備用線路,否則它們會被擠在一起。 – person101 2014-11-22 08:44:28

回答

0

與功能的問題是,你有一個名爲num一個全局變量,但你main功能也有一個名爲num的局部變量。 main中的num += 1行只改變局部變量。但if num != 1在最後檢查全局變量。

爲了解決這個問題,添加一個全局聲明:

def main(): 
    global num 
    # the rest of your code 

爲什麼這項工作?

在Python,你寫一個函數賦值語句(如num = 0num += 1)的任何時間,創建一個局部變量,除非你明確告訴它不要,具有global聲明。*所以,並稱global num表示現在沒有本地變量num,所以num += 1反而影響全局。

這在教程部分Defining Functions中有更詳細的解釋。

*或nonlocal聲明,但您不想了解這一點。


但是,有一個更好的方法來解決這個問題。而不是使用全局變量,您可以return本地變量中的值。像這樣:

def main(): 
    # your existing code 
    return num 

# your other functions 

score = main() 
if score != 1: 
    #if player guesses incorrectly they get told the correct awnser 
    print('The number I was thinking of was...',comp_num,'!\n') 
+0

我已經嘗試了這兩種方法,但他們都沒有工作我仍然有同樣的問題。如果玩家再次玩,計算機號碼也保持不變。這是因爲comp_num需要在main()中設置嗎? – person101 2014-11-22 08:59:41

+0

@Hannah_sfc:首先,我剛剛測試了兩個更改的代碼,最初的問題肯定會消失。你可以[在線運行](http://goo.gl/fM91c9)並親自查看。同時,是的,「comp_num」永不改變的原因是你沒有任何代碼在初始啓動後設置它。在'main'中設置它將會起作用 - 但是如果你想把它作爲一個全局的,你需要另一個'global'語句;否則「我想到的號碼」行會顯示錯誤的值。 – abarnert 2014-11-24 20:25:29

-1

你可以試試這個代碼。我做到了一所學校分配

import random 
print "Welcome to guess my number!" 

number = random.randint(1,100) 

count = 1 
while count <= 5: 
    guess = int(raw_input("Guess an integer between 1 and 100 (inclusive): ")) 
    if guess == number: 
     print "Correct! You won in",count,"guesses!" 
     break 
    if guess > number: 
     print "Lower!" 
    else: 
     print "Higher!" 
    count += 1 

if count == 6: 
    print "Man, you really suck at this game. The number was", number 
+0

非常感謝。這與我開始使用的代碼類似,不過,我的老師希望我可以隨時使用變量,當考試功能達到較高分數時。 – person101 2014-11-22 08:49:29

+0

我也有它的功能版本。但是你的程序沒有明確使用函數。 – Beginner 2014-11-23 01:06:39

0

好吧,讓我的朋友看着我的代碼,我們一起解決了它。我們已經確定這兩個數字保持不變,並通過這樣做被告知正確的答案。

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

我想感謝所有幫助過我的人,我仍然無法在代碼中看到問題,更不用說修復它了。出於這個原因,當我發現錯誤時,我會將@abarnert的答案標記爲已接受的答案。

+0

只是「*此代碼現在可用」的轉儲*不是有用的答案;請添加一個解釋問題是什麼以及你做了什麼來解決它。 – jonrsharpe 2014-11-22 12:09:47

+0

abarnets答案是接受的答案,因爲這是問題,我已經包含我的代碼,因爲它在一個示例中顯示它。 – person101 2014-11-22 12:32:55