为什么我的猜谜游戏会跳过“低等”游戏。和更高的'部分

时间:2017-10-05 03:29:32

标签: python

import random
import time
numOfGuesses = 0
guess = ''
playername = ''
numbertoguess = 0
MAX_GUESS = 10
#======================
playername = input ('What is your name:')
numbertoguess = random.randint (1, 100)
input("hello, " + playername + ", Guess the number I am thinking of   
(hint     its     between 1 and 100")
#======================
while numOfGuesses < MAX_GUESS:
 guess = int(input("What is your guess:"))
numOfGuesses += 1
 time.wait(1)
if guess < numbertoguess:
print ('Higher')
if guess > numbertoguess:
 print ('Lower')
elif numOfGuesses > MAX_GUESS:
 sys.exit()
else:
   sys.exit()
 #======================
 if guess == numbertoguess:
 print ("You are right," + playername + ",you guessed it in "+str  
(numOfGuesses) + "tries")
 elif guess != numbertoguess and numOfGuesses == 10:
print ("awe so close," + playername + ".")
print ("the number was" + str(numbertoguess) +".")`

当你完成一个猜测而不是告诉你去&#34;更高&#34;或&#34;降低&#34;代码在它们上面运行,也不打印。如果有人能帮助我,那我就是陌生人。

1 个答案:

答案 0 :(得分:0)

好的,这里有一些问题。

1) Python将换行符视为C语言中的分号,所以

input("hello, " + playername + ", Guess the number I am thinking of   
(hint     its     between 1 and 100")

会抛出语法错误。要编码换行符,请使用转义序列"\n"

input("hello, " + playername + ", Guess the number I am thinking of \n(hint it's between 1 and 100")

这看起来像是复制+粘贴问题。

2) Python使用缩进来确定块的位置。因此,循环体中的所有语句必须以与其余部分相同数量的空格/制表符开头。所以你的while循环应该看起来像(缩进明智)

while numOfGuesses < MAX_GUESS:
    guess = int(input("What is your guess:"))
    numOfGuesses += 1
    time.sleep(1)

    if guess < numbertoguess:
        print ('Higher')
    if guess > numbertoguess:
        print ('Lower')
    elif numOfGuesses > MAX_GUESS:
        sys.exit()
    else:
        sys.exit()

我认为这会导致您在问题标题中指定的问题。由于while循环只执行行guess = int(input("What is your guess:")),因为它是唯一正确缩进的行。

注意:你不能混合标签和空格,python会有一个合适的no soup for you

缩进样式通常为4个空格或1个制表符。单个空格缩进会让您在一段时间后感到头痛。

3)如果您需要延迟,正确的功能是time.sleep()

4)你的身体中有两个if语句,所以如果猜测通过if guess < numbertoguess:它会继续到下一个if guess > numbertoguess:并失败。然后它将跳转到else主体,这是一个系统退出/中断语句。要么会导致游戏过早结束。

将链变为:

if guess < numbertoguess:
    print ('Higher')
elif guess > numbertoguess:
    print ('Lower')
elif numOfGuesses > MAX_GUESS:
    break;
else:
    break;

5)您有sys.exit(),但忘了import sys。此外,exit()不需要导入,您可以在没有sys模块的情况下使用它。

6) exit()退出程序。如果其中一个elif / else语句执行,则在while循环之后将运行任何内容。您正在寻找的语句可能是break语句,它会在循环后的下一行继续执行程序。

7)与数字1相同),你有一条声明分为两行

print ("You are right," + playername + ",you guessed it in "+str  
(numOfGuesses) + "tries")

修复

print ("You are right," + playername + ",you guessed it in " + str(numOfGuesses) + "tries")

注意

样式方面,使用4个空格或1个制表符缩进。它使事情更容易阅读。还可以使用换行符分隔代码中的逻辑块。您可以使用#===========来表示重要的块或巨大的逻辑块。

这并不是说您不能没有新行,也不能将#===========用于逻辑块,但阅读代码的人会讨厌您。

你的字符串在这里和那里缺少一些格式

你已经在底部对最大猜测进行了硬编码:elif guess != numbertoguess and numOfGuesses == 10:事实上,你并不需要检查,因为你已经检查了上面的正确答案。

if guess == numbertoguess:
    print ("You are right," + playername + ",you guessed it in " + str(numOfGuesses) + "tries")

else:    
    print ("awe so close," + playername + ".")
    print ("the number was" + str(numbertoguess) +".")

这是有效的,因为用户现在可以使用两种逻辑状态:正确猜测或错误猜测MAX_GUESS次。如果您有3个以上的逻辑状态(猜对了,猜错了,猜测复活节彩蛋有42个),您将不得不再检查一次。

考虑使用for循环而不是while循环。当你不需要知道你做了多少循环,或者你的循环条件是一个特定的布尔表达式时,while循环是很好的。 (ex while something.hasNext():

for循环适用于特定次数的迭代,或者当您需要按顺序访问某些内容时。 (每个循环也有。)

for i in range(MAX_GUESS): # i = 0 to i = MAX_GUESS -1

然后你不需要检查你的猜测次数,因为你保证最多可以循环MAX_GUESS次

我是否可以提出建议。获取具有语法突出显示和检查功能的IDE(集成开发环境)。我使用Eclipsepython插件,但eclipse对于初学者来说有点多。我们的CS教授建议Wing,但我从未使用它

---

成品(除了字符串格式。我会让你这样做):

import random
import time

numOfGuesses   = 0
numbertoguess  = 0
MAX_GUESS      = 10
guess          = ''
playername     = ''

playername = input ('What is your name:')
numbertoguess = random.randint (1, 100)
input("hello, " + playername + ", Guess the number I am thinking of \n(hint it's between 1 and 100")

for numOfGuesses in range(MAX_GUESS): # nOG = 0 to nOG = MAX_GUESS -1
    guess = int(input("What is your guess:"))
    time.sleep(1)

    if guess < numbertoguess:
        print ('Higher')
    elif guess > numbertoguess:
        print ('Lower')
    else:
        break;

if guess == numbertoguess:
    print ("You are right," + playername + ",you guessed it in " + str(numOfGuesses) + "tries")

else:    
    print ("awe so close," + playername + ".")
    print ("the number was" + str(numbertoguess) +".")