在Guess中重新启动选项Python中的数字

时间:2013-06-22 01:08:18

标签: python python-3.x

我很难通过这个Guess The Number任务。我似乎无法做到正确,并希望得到一些帮助。再次播放选项无法正常工作,因为当您赢得游戏并选择是再次播放时,它不会重新启动游戏,它将继续您的下一个猜测数字(例如:猜测数字1,猜测数字2 ......)。但是当你输了它时会回到第一部分,它会问你“想要多少次尝试”。那么我怎样才能获得胜利的部分呢?另外,有人可以给我一些关于如何改进提示选项的提示吗?另外,我是否需要插入任何 def main():的东西?因为我不确定我们是否已经完成了这项任务。所以请帮帮我。感谢。

# This is a guess the number game.
import random
answer = "yes"
number = random.randint(1, 10)

while answer == 'yes':
    tries = 0
    guess = 0
    guessnumber = 0
    tries = int(input("How many tries would you like? "))
    tries2 = tries
    while guessnumber != tries2:
            guessnumber += 1
            guess = int(input("Enter guess number " + str(guessnumber) + ": "))
            if guess < number:
                print("You guessed too low")
                hint = input("Would you like a hint, yes or no?: ")
                if hint == "no":
                    continue
                    print("The number is less than", number + 1)
                if guess > number:
                    print("You guessed too high")
                    hint = input("Would you like a hint, yes or no?: ")
                    if hint == "no":
                        continue
                    print("The number is greater than", number - 1)
                if guess == number:
                    tries = str(tries)
                    print("YOU WON!")
                    answer = input("Play again? Yes/No? ") `enter code here`
                    if answer == 'no':
                        break
            if guess != number:
                number = str(number)
                print("GAME OVER!!! The number I was thinking of was " + number)
                answer = input("Play again? Yes/No? ")
                if answer == "no":
                    break

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用函数。这是一些可能的伪代码:

def game():
    tries = 0
    guess = 0
    ...
    if guess != number:
        number = str(number)
        print("GAME OVER!!! The number I was thinking of was " + number)

 answer = "yes"
 while answer == 'yes':
     game()
     answer = input("Play again? Yes/No? ")

功能有助于将问题分解为更小和更易维护的部件。

对于此代码,您肯定可以在代码中重置变量,但不建议这样做。

P.S。它可以理解它是什么游戏,但更好的写入描述,而不只是写“赋值”。更明确的问题是,你更有可能得到答案。

答案 1 :(得分:1)

有几件事:

random.randint(1,1)表示产生1到1之间的随机整数。它总是给出1。

为什么只设置一次“尝试”副本?

Python MATTERS中的缩进。它会更改if / elif / else分支的计算方式。因此(猜测&gt;数字)和(guess == number)检查是以(猜测&lt;数字)为条件的,这是不可能的。鉴于这些状态:(guess&lt; number),(guess&gt; number)和(guess == number)相互排斥并比较相同的变量,最好将后续if块更改为elifs。

“GAME OVER”应该只在尝试用尽后触发,因此它不能在(guessnumber&lt; attempts)循环内。

# This is a guess the number game.
import random
answer = "yes"
number = random.randint(1, 100)

while answer != 'no':
    guess = 0
    guessnumber = 0
    tries = int(input("How many tries would you like? "))
    while guessnumber < tries:
            guessnumber += 1
            guess = int(input("Enter guess number " + str(guessnumber) + ": "))
            if guess < number:
                print("You guessed too low")
                hint = input("Would you like a hint, yes or no?: ")
                if hint == "no":
                    continue
                print("The number is less than", number + 1)
            elif guess > number:
                print("You guessed too high")
                hint = input("Would you like a hint, yes or no?: ")
                if hint == "no":
                    continue
                print("The number is greater than", number - 1)
            elif guess == number:
                print("YOU WON!")
                answer = input("Play again? Yes/No? ") #enter code here
                break
    if (guess != number) and (guessnumber >= tries):
        number = str(number)
        print("GAME OVER!!! The number I was thinking of was " + number)
        answer = input("Play again? Yes/No? ")