简体   繁体   中英

Python 3.6 need help using randint

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")

How would I implement when I tell the bot to guess higher, his range goes from (1, 100) to between his guess + 1 and 100? And vise versa for when I tell him to guess lower. Semi new to python.

Also any tips on the look and flow of my code are much appreciated!

You could store the range of the guess in variables, ie minGuess and maxGuess . You can set them to 1 and 100 respectively at the beginning. Now when you tell the bot to guess higher, just set the minGuess variable to BotGuesss + 1 . Of course everytime you have BotGuess = randint(1, 100) in your code, you replace that with BotGuess = randint(minGuess, maxGuess) .

Also a tip on your code in general: Variables should usually begin with a lowercase letter. Your variables start with an uppercase letter, which usually indicates a Class. This is practical so you can understand code of others easier. Here is a complete style guide.

Instead of giving fixed parameters to randint , you can use variables which you change in your conditionals:

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")

A test run gave me this output:

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

nbro recommended in a comment that you improve the style of your writing. He was saying that your code should be formatted more like this instead:

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()

If you want a program that is somewhat closer to what you are asking for, the following code may help you better in changing your own to make a smarter robot:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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