Python 3.6需要使用randint的帮助

时间:2017-02-24 15:18:51

标签: python python-3.x loops numbers operators

from random import randint

Secret = randint(1, 100)

Chances = 5

BotGuess = randint(1, 100)

while(BotGuess != Secret and Chances != 0):
    BotGuess = randint(1, 100)
    print("Bot: I guess " + str(BotGuess) + "!")
    Chances -= 1

    if (BotGuess > Secret):
        print("You: Nope! Try guessing lower")

    elif (BotGuess < Secret):
        print("You: Nope! Try guessing higher")

    elif (BotGuess == Secret and Chances > 0):
        print("You: Wow! You win.")

    elif (Chances == 0):
        print("You: You're out of chances! You lose")


    else:
        print("Shouldn't be possible")

当我告诉机器人猜测更高,他的范围从(1,100)到他的猜测+ 1和100之间时,我将如何实施?当我告诉他猜低时,反之亦然。半新的python。

我的代码外观和流程的任何提示都非常感谢!

3 个答案:

答案 0 :(得分:1)

您可以将猜测范围存储在变量中,即minGuessmaxGuess。您可以在开头分别将它们设置为1100。现在,当您告诉机器人猜测更高时,只需将minGuess变量设置为BotGuesss + 1即可。当然,每当您的代码中有BotGuess = randint(1, 100)时,您都会将其替换为BotGuess = randint(minGuess, maxGuess)

一般也是代码的提示:变量通常应以小写字母开头。您的变量以大写字母开头,通常表示一个Class。这很实用,因此您可以更轻松地理解他人的代码。 Here是完整的风格指南。

答案 1 :(得分:1)

您可以使用在条件中更改的变量,而不是将固定参数提供给randint

from random import randint

Secret = randint(1, 100)

Chances = 5

min_guess = 1
max_guess= 100

BotGuess = randint(1, 100)

while(BotGuess != Secret and Chances != 0):
    BotGuess = randint(min_guess, max_guess)
    print("Bot: I guess " + str(BotGuess) + "!")
    Chances -= 1

    if (BotGuess > Secret):
        print("You: Nope! Try guessing lower")
        max_guess = BotGuess - 1

    elif (BotGuess < Secret):
        print("You: Nope! Try guessing higher")
        min_guess = BotGuess + 1

    elif (BotGuess == Secret and Chances > 0):
        print("You: Wow! You win.")

    elif (Chances == 0):
        print("You: You're out of chances! You lose")


    else:
        print("Shouldn't be possible")

测试运行给了我这个输出:

Bot: I guess 68!
You: Nope! Try guessing higher
Bot: I guess 74!
You: Nope! Try guessing higher
Bot: I guess 90!
You: Nope! Try guessing higher
Bot: I guess 93!
You: Nope! Try guessing lower
Bot: I guess 92!
You: You're out of chances! You lose

答案 2 :(得分:1)

nbro在评论中建议您改进写作风格。他说你的代码格式应该更像这样:

import random


def main():
    secret = random.randint(1, 100)
    chances = 5
    bot_guess = random.randint(1, 100)
    while bot_guess != secret and chances > 0:
        bot_guess = random.randint(1, 100)
        print(f'Bot: I guess {bot_guess}!')
        chances -= 1
        if bot_guess > secret:
            print('You: Nope! Try guessing lower.')
        elif bot_guess < secret:
            print('You: Nope! Try guessing higher.')
        elif bot_guess == secret and chances > 0:
            print('You: Won! You win.')
        elif chances == 0:
            print('You: You are out of chances! You lose.')
        else:
            raise ValueError('should not be possible')


if __name__ == '__main__':
    main()

如果您想要一个与您要求的程序更接近的程序,以下代码可以帮助您更好地更改自己的程序以制作更智能的机器人:

import math
import random
import time


LOW_VALUE = 1
HIGH_VALUE = 100
CHANCES = math.ceil(math.log(HIGH_VALUE - LOW_VALUE, 2))


def main():
    secret = random.randint(LOW_VALUE, HIGH_VALUE)
    robot = random.Random(math.pi * time.time())
    lo, hi = LOW_VALUE, HIGH_VALUE
    for _ in range(CHANCES):
        guess = robot.randint(lo, hi)
        print(f'Bot: I guess {guess}!')
        if guess < secret:
            print('You: Nope! Try guessing higher.')
            lo = guess + 1
        elif guess > secret:
            print('You: Nope! Try guessing lower.')
            hi = guess - 1
        else:
            print('You: Won! You win.')
            break
    else:
        print('You: You are out of chances! You lose.')


if __name__ == '__main__':
    main()