Python猜测游戏即使玩家得到正确答案,也可以打印出正确的答案

时间:2014-11-21 23:46:05

标签: python function

所以我最近开始编写python,这个代码有一个问题。如果玩家在使用完毕后得到的答案不正确,那么它应该打印出它所做的答案,但只有在第一次播放时我才会再次播放,如果错误则不会告诉他们正确的答案。当玩家得到正确答案时,它也会这样做。当您再次使用播放功能时,计算机选择的数字保持不变。请尽量帮助我,但请记住,我的理解手段在python的某些方面非常有限。我已经收录了很多评论,以帮助其他人了解最新情况。我已经包含了我的代码以及我在shell中获得的内容。

代码:

#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!

3 个答案:

答案 0 :(得分:0)

您的函数存在的问题是您有一个名为num的全局变量,但您的main函数还有一个名为num的局部变量。 num += 1内的main行只会更改局部变量。但是最后的if num != 1检查全局变量。

要解决此问题,请添加全局声明:

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

为什么这样做?

在Python中,每次在函数中编写赋值语句(如num = 0num += 1)时,都会创建一个局部变量 - 除非你明确告诉它不要,用{ {1}}声明。*因此,添加global表示现在没有局部变量global num,因此num会影响全局变量。

Defining Functions上的教程部分对此进行了更详细的说明。

*或num += 1声明,但您还不想了解。


然而,有一种更好的方法来解决这个问题。您可以nonlocal局部变量中的值,而不是使用全局变量。像这样:

return

答案 1 :(得分: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的回答标记为接受的答案,因为他发现了这个错误。

答案 2 :(得分:-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