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

时间:2018-01-21 20:52:30

标签: python python-3.x

我有一个数字游戏的代码,我看不出我做错了什么,但每当我运行时它告诉我,无论我选择什么数字,我都太低了。有人可以告诉我我做错了什么吗?谢谢!另外,我知道有些部分可能是多余的,但我不知道它们是否实际上阻止了代码的运行。

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

3 个答案:

答案 0 :(得分:0)

我注意到的一件事是

<div id="mainHero" style="background-image:url(someURL);">
    <div class="container">
        <div id="heroBox" class="">
            <h1>Telehealth Through Teladoc</h1>
        </div>
    </div>
</div>

    #heroBox {
        position: absolute;
        top: 50%; 
        left: 0;
        width: auto; // doesn't work
        max-width: 35%;
        transform: translateY(-50%);
        color: $color-text-offwhite;
        text-shadow: 1px 1px 1px rgba(0,0,0,0.50);
        background-color: #000;
        background: rgba(0, 0, 0, 0.7);
        padding: 10px 30px; // it is not the padding

        h1 {
            margin: 0;
            text-transform: none;
            font-size: 2.5rem; 
            // display: inline; // doesn't work
            // word-wrap: break-work; // doesn't work
        }
    }

    .container {
        position: relative;
        z-index: 100;
    }

因为前两个if语句永远不会执行,因为 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 与前两个条件相矛盾。

答案 1 :(得分:0)

对于计算机,不可能生成真正随机的数字。相反,我们使用一种名为播种的方法将不断变化的值(例如当前时间)提供给随机数生成器,以便每次都返回不同的值。

修改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对象,以便每次执行代码都会产生不同的伪随机值。

答案 2 :(得分:0)

您的代码结构如下所示

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())转换为整数,那么当您调用此整数变量时,您不必一次又一次地执行此整数转换。