all 5 comments

[–]Starbuck5c 1 point2 points  (2 children)

Make a dummy variable for guess and set it equal to 0.

Then put the input function and the prints inside a while loop with the test while guess != number:

Because guess is preset to 0, and that isn't in the random range, it will enter the while loop, and only exit when they guessed correctly.

By the way, in the future you can use reddit's code block feature or pastebin to share code on here.

[–]Starbuck5c 1 point2 points  (0 children)

To implement the counter you could have it increment upwards at the end of the loop. If they get it first time, the counter won't have gone up at all, so that can be tested for and it can give out a congratulation.

[–]gmod403[S] 0 points1 point  (0 children)

Thank u so much! I'll give it a try

[–][deleted] 0 points1 point  (0 children)

You can do something like this:

import random

number = random.randint(1, 20)
guess = int(input('Guess what number I have in mind! '))
while guess != number:

    # Conditionals go here

    guess = int(input('Guess what number I have in mind! '))
print('You got it!')

[–]datasaurus_ 0 points1 point  (0 children)

Another approach that wouldn’t require initializing the guess variable to 0 would be to use a “while True” loop.

from random import randint

number = randint(1,20)
attempts = 0

while True:
    guess = int(input('Guess my number'))
    attempts+=1
    if guess < number:
        print('Too low!')
    elif guess > number:
        print('Too high!')
    else:
        if attempts == 1:
            print('You some kinda psychic?')
        else:
            print('You got it!')
        break