我想通过Python为我的女朋友写一个非常简单的猜数游戏。但我相信这是一个非常合乎逻辑的问题,所以任何不懂Python的人都可以读到这个!这个游戏非常标准:计算机随机选择一个数字并要求用户猜测。这是我的代码:
while True:
from random import randint
ans = randint(1,100)
print "Guess from 1 to 100"
bool = True
while bool:
num = input()
if num < ans:
print str(num) + " is too small!"
if num > ans:
print str(num) + " is too BIG!"
if num == ans:
print "Bingo! %d is what I am thinking!" % num
print "Try again =]"
for i in range(0,40):
print '*',
print
bool = False
但是,我想在其中添加一些新功能:
我知道这是一个难题,但这对我女朋友来说是一个重要的礼物,所以如果有人能提供帮助,我真的很感激。 Python代码对我来说不是必须的,所以如果有人能通过告诉我算法来回答我的第三个问题,我认为我可以自己编写代码。感谢读这篇文章的每个人!
答案 0 :(得分:1)
试试这个。 getInput()函数将验证您的所有三个问题。 raw_input()函数接受你给它的任何东西,这很好,因为你可以输入像&#39; cheat&#39;这样的命令。和&#39;退出&#39; try&amp; except语句允许您捕获不是数字(或整数)的猜测。顺便说一下,没有必要创建一个循环来创建一个重复的字符串,你可以简单多次因为python很棒! (它也更快,但这对于少量代码来说并不是很重要)享受!
def getInput(minValue,maxValue):
while True:
try:
num = raw_input("Enter your guess: ")
if num == 'cheat':
return num
if num == 'exit':
return num
num = int(num)
if num < minValue or num > maxValue:
print "That is a silly guess! Your number is between %d and %d." % (minValue, maxValue)
assert False
else:
return num
except:
print "That is not a valid guess!"
exit = False
while not exit:
from random import randint
minValue = 1
maxValue = 100
ans = randint(minValue,maxValue)
print "Guess from %d to %d" % (minValue, maxValue)
while True:
num = getInput(minValue, maxValue)
if num == 'cheat':
print "I was thinking of %d. Cheater!" % ans
break
if num == 'exit':
print "Exiting program!"
exit = True
break
if num < ans:
print str(num) + " is too small!"
minValue = num
if num > ans:
print str(num) + " is too BIG!"
maxValue = num
if num == ans:
print "Bingo! %d is what I am thinking!" % num
print "Try again =]"
print "* "*40
print
break
答案 1 :(得分:0)
对于第二个问题,只需检查猜测是否是&#34;作弊&#34;。如果是,请打印ans
并打破。
对于第三个问题,您可以设置一个包含最小值和最大值的列表。每次猜测时,都要更新它们的值:
guess_range = [1, 100]
if guess < guess_range[0]:
# The guess is silly, print so
elif guess < ans:
# The guess is not silly but is lower than ans, update range
guess_range[0] = guess
# Do the same to guess_range[1] changing comparisons.
答案 2 :(得分:0)
第一个问题:
我不认为有一个很好的方法,但你可以使用这个功能:
import msvcrt
def invisible():
key=''
inpt=''
while True:
key = msvcrt.getch()
if key == '\r': break
inpt += key
msvcrt.putch(' ')
return inpt
然后,使用它而不是input
。
第二个问题:
只需检查invisible
函数的返回值是"cheat"
还是您想要作为欺骗词的任何内容
第三个问题:
像@xbello说的那样。你可以有两个包含范围的变量。例如,minVal
,maxVal
然后,在每次迭代中,检查输入并更新它们。
#assuming inp is integer
if (inp >= maxVal) or (inp <= minVal):
print "silly"
else:
maxVal = max(maxVal,inp)
minVal = min(minVal,inp)
答案 3 :(得分:0)
这个怎么样?
import random
def main():
ans = random.randint(1,100)
guess = -1
cheat = "cheat"
print "Guess from 1 to 100"
guessed = []
lower_bound = 1
upper_bound = 100
while guess != ans:
guess = input(">> ")
if guess == cheat:
print "The answer is: ", str(ans)
if not validate(guess, lower_bound, upper_bound):
print "Silly, the number is between ", str(lower_bound), " and ", str(upper_bound)
continue
if guess in guessed:
print "You've already guessed that!"
continue
guessed.append(guess)
if guess < ans:
lower_bound = max(lower_bound, guess)
print "Too low!"
elif guess > ans:
upper_bound = min(upper_bound, guess)
print "Too high!"
if guess == ans:
print "That's right!"
return
def validate(guess, lower_bound, upper_bound):
return lower_bound < guess < upper_bound
if __name__ == "__main__":
play = 'y'
while play in ('y','Y'):
main()
play = raw_input("Play again? (y/n): ")