1

我很难完成这个猜数字作业。我似乎无法正确处理并需要一些帮助。再次游戏选项不起作用,因为当您赢得游戏并选择“是”再次游戏时,它不会重新开始游戏,它会继续您的下一个猜数(例如:猜数 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
4

2 回答 2

1

有几件事:

random.randint(1, 1) 是说产生一个介于 1 和 1 之间的随机整数。它总是给出 1。

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

Python 中的缩进很重要。它改变了 if/elif/else 分支的评估方式。所以 (guess > number) 和 (guess == number) 检查是有条件的 (guess < number) 这是不可能的。鉴于这些状态:(guess < number)、(guess > number) 和 (guess == number) 是互斥的并且比较相同的变量,最好将后续的 if 块更改为 elifs。

"GAME OVER" 应该只有在尝试次数用尽后才会触发,所以它不能在 (guessnumber <trys) 循环内。

# 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? ")
于 2013-06-22T01:54:13.927 回答
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? ")

函数有助于将问题分解为更小、更易于维护的部分。

对于此代码,您当然可以重置代码中的变量,但不建议这样做。

PS 可以理解它是什么游戏,但最好写描述 if 而不是只写“作业”。更明确的问题是,你更有可能得到答案。

于 2013-06-22T01:44:44.727 回答