[英]why does my guessing game skip over the 'lower' and 'higher' parts
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) +".")`
当您完成一个猜测而不是告诉您“更高”或“更低”时,代码会运行它们并且也不会打印。 如果有人可以帮助我,我对 python 很陌生,那就太好了。
好的,这里有几个问题。
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 会适合你,没有汤
此外,缩进样式通常为 4 个空格或 1 个制表符。 一段时间后,单个空格缩进会让您头疼。
3)如果你需要延迟,正确的函数是time.sleep()
4)你的 while 主体中有两个 if 语句,所以如果猜测通过if guess < numbertoguess:
它将继续下一个if guess > numbertoguess:
并且失败。 然后它会跳转到else
主体,这是一个系统退出/中断语句。 要么会导致游戏提前结束。
将 if 链更改为:
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 个制表符缩进。 它使事情更容易阅读。 还可以使用换行符来分隔代码中的逻辑块。 logical blocks.您可以使用#===========
来表示重要的块或逻辑块。
这并不是说你不能没有换行符,也不能对逻辑块使用#===========
,但阅读你的代码的人会讨厌你。
您的字符串在这里和那里缺少一些格式
您已经在底部对最大猜测进行了硬编码: 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
循环适用于您不需要知道已经完成了多少循环,或者当您的循环条件是特定的布尔表达式时。 (例如while something.hasNext():
)
for
循环适用于特定次数的迭代,或者当您需要按顺序访问某些内容时。 (每个循环也有)。
for i in range(MAX_GUESS): # i = 0 to i = MAX_GUESS -1
然后你不需要检查你的猜测次数,因为你保证最多循环 MAX_GUESS 次
如果可以的话,给个建议。 获取具有语法突出显示和检查功能的 IDE(集成开发环境)。 我使用带有python插件的Eclipse ,但 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) +".")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.