繁体   English   中英

无法在Python 3中更正我的高低数字游戏吗?

[英]Can't correct my high-low number game in Python 3?

我有一个用于数字游戏的代码,我看不到自己在做错什么,但是无论何时我运行它,它都会告诉我无论选择什么数字,我都太低了。 有人可以告诉我我在做什么错吗? 谢谢! 另外,我知道某些部分可能是多余的,但我不知道它们是否实际上在阻止代码正常工作。

import random
print("Welcome, to the worst game you will ever play. For some reason, you've got to pick a number between 1 and 10. "
      "Pick 11, I dare you.")
print("The aim is to win with the lowest possible score.")
score = 0
player_attempts = 0
play = True

while play:
    computer_number = random.randint(1, 11)
    player_number = int(input("OK. What do you think the number is?"))
    player_number = int(player_number)

    while player_number != computer_number:

        if player_number == computer_number and player_attempts == 0:
                print("Wow. You actually did it. And it only took you " + 
                      str(player_attempts) + "try. I'm impressed. I thought you were just one of those weirdos who "
                      "downloads a dodgy free game to escape from society...")
                score += 1
                player_attempts += 1
                print("Your score is " + str(score) + ". Maybe. I could be lying. How would you know?")
                play = False

        elif int(player_number) == int(computer_number) and int(player_attempts) >= 0:
                    print("Wow. You actually did it. And it only took you " + str(player_attempts) +
                          "tries. I'm impressed. I thought you were just one of those weirdos who "
                          "downloads a dodgy free game to escape from society...")
                    score += 1
                    player_attempts += 1
                    print("Your score was " + str(score) + ". Maybe. I could be lying. How would you know?")
                    play = False

        elif int(player_number) > int(computer_number):
            print("You overshot. But really, does it matter? You should stop, get out of the basement, enter society. "
                  "Or have another go.")
            score += 1
            player_attempts += 1
            print("Your score is " + str(score) + ".")
            again = str(input("Try again."))
            if again == "no":
                play = False
            elif again == "yes":
                play = True

        elif int(player_number) < int(computer_number):
            print("You were too low. Underachieving. Sound familiar?")
            score += 1
            player_attempts += 1
            print("Your score is " + str(score) + ".")
            again = str(input("Try again."))
            if again == "no":
                play = False
            elif again == "yes":
                play = True

        elif int(player_number) == 11:
            print("Wow. You really chose 11. You are actually more intelligent than I had originally thought...")
            again = str(input("Try again."))
            if again == "no":
                play = False
            elif again == "yes":
                play = True

我注意到的一件事是

    while player_number != computer_number:

         if player_number == computer_number and player_attempts == 0:
            print("Wow. You actually did it. And it only took you " + 
                  str(player_attempts) + "try. I'm impressed. I thought you were just one of those weirdos who "
                  "downloads a dodgy free game to escape from society...")
            score += 1
            player_attempts += 1
            print("Your score is " + str(score) + ". Maybe. I could be lying. How would you know?")
            play = False

         elif int(player_number) == int(computer_number) and int(player_attempts) >= 0:
                print("Wow. You actually did it. And it only took you " + str(player_attempts) +
                      "tries. I'm impressed. I thought you were just one of those weirdos who "
                      "downloads a dodgy free game to escape from society...")
                score += 1
                player_attempts += 1
                print("Your score was " + str(score) + ". Maybe. I could be lying. How would you know?")
                play = False

因为前两个if语句将不会执行,因为while player_number != computer_number:与前两个条件相反。

对于计算机,不可能生成真正的随机数。 取而代之的是,我们使用一种称为种子的方法将不断变化的值(例如当前时间)馈送到随机数生成器,以便每次返回不同的值。

修改while循环以播种random生成器对象,如下所示:

while play:
    random.seed()
    # This seeds the random number generator with the system time.
    computer_number = random.randint(1, 11)
    player_number = int(input("OK. What do you think the number is?"))
    ...

这应该使用系统时间初始化random对象,以便每次执行代码都会产生不同的伪随机值。

您的代码结构如下所示

while play:
    computer_number = randomnumber
    player_number = inputnumber

    while player_number != computer_number:    #not equal
        if condition1:
            congratulation
        elif condition2:
            be sarcastic
        elif condition3:
            be even more sarcastic

现在,如果您立即猜出该数字,则可以避免内部while循环,而转到下一个计算机生成的随机数。 但是,如果您选择与计算机不同的数字,则会进入内循环,并且永远被困在那里,因为您不会更改player_numbercomputer_number 解决方案? 摆脱内循环。 完全没有必要,因为您可以使用if-elif构造检查此条件。

附带说明: input()的分配变量已经是一个字符串变量,无需将其与str(input()转换。如果将int(input())转换为整数,则不必调用此整数变量时,不必一遍又一遍地执行此整数转换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM