Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Python 3.3.0 storing new random values

$
0
0
I was wondering how to store a new random value, since I am trying to make a guessing game. Where user guesses a number. I created a while loop where the loop will only break if the user gets number correct. But the problem is that when I generate a random number, it will store the same value until you guessed correct.

Output:
Guess a number: 28
Sorry the number was: 29

And it will keep doing that until, you guessed the right number. Therefore my question is how to update the random number, every time when the person guesses the right number?

import random
random_number, lucky_number = random.randint(0,100), 0

lucky_number = int(input("Guess a number:"))

if lucky_number != random_number:
    while random_number != lucky_number:
            lucky_number = int(input("Guess a number:"))
            print("The number was %d." %(random_number))

elif random_number == lucky_number:
    print("You have guessed the right number!")
    break

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>