2014-11-21 71 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