samedi 26 novembre 2016

Random number beeing changed within Python script

I'm working on a Python script where a user has to guess a random number, selected by the script. This is my code:

import random
while True:
    number = random.randint(1, 3)
    print("Can you guess the right number?")
    antwoord = input("Enter a number between 1 and 3: ")
    if antwoord == number:
        print ("Dang, that's the correct number!")
        print (" ")
    else:
       print ("Not the same!")
       print ("The correct answer is:")
       print (number)

    while True:
        answer = input('Try again? (y/n): ')
        print (" ")
        if answer in ('y', 'n'):
            break
        print("You can only answer with y or n!")
    if answer == 'y':
        continue
    else:
        print("Better next time!")
        break

It works... Sort of... I was trying it and came across this: User enters 2, it says it's incorrect, but then displays the same number!

I have the feeling that, every time I call the variable 'number', it changes the random number again. How can I force the script to hold the random number picked at the beginning, and not keep changing it within the script?




Aucun commentaire:

Enregistrer un commentaire