0
import time
import random


# USER CHOOSES TO PLAY
play = input("Type 'Play' to play")


while play == "play":
    # COMPUTER GENERATES NUMBER
    number = random.randint(1,11)

    # USER GOES TO GUESS THE NUMBER
    print ("guess the number, the computer has picked a number between 1 and 10. You have 5 tries")
    num_guess = int(input())
    
    # CODE TELLS USER IF THEY CHOOSES THE RIGHT OR WRONG NUMBERPL
    if num_guess == number:
        print(f"Yes the number is {number}, good job!")
    elif num_guess != number:
        for numguesses in range (4, 0, -1):
            num_guess = int(input(f"guess the number you have {numguesses} tries"))



play = input("Type 'Play' to play")

i have no idea whether when the computer generates a number with 'random.randint' if it is generating a new number every time i guess what number it has generated (as this is a number generating game). Please can you help and let me know.

Im trying to make it so there is only 1 number generated, from 1,11, and i have 5 guess to guess the right number, then it repeats if the user types 'play'

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 2
    It generates a new random number every time it hits the `random.randint(1,11)` line. – deceze Jan 15 '23 at 21:24
  • But it is preferable to use a [secure random generator](https://stackoverflow.com/q/20936993/589259), which generally uses a secure random number from the operating system (as starting point / seed). But otherwise yes, it's a new random number everytime. Of course, it can be the same value as before by chance alone. It's **not** [this one](https://xkcd.com/221/) :) – Maarten Bodewes Jan 15 '23 at 21:29

1 Answers1

0

The first comment correctly answers the question.

  • the code generates a new random number every time the line random.randint(1,11) is encountered.
  • not only this, but you can easily prove this to yourself by adding the following to the next line:
print('the number has been generated:', number)

which will print the new number to the console each time...

D.L
  • 4,339
  • 5
  • 22
  • 45