How do you rest your variable everytime your execute the program.

for x in range(1,100):
    running = True

while running:
    guess = int(raw_input("Enter an integer:"))

    if guess == x:
        print "Nice"
        running = False

    if guess < x:
        print "Higher"
        running = True

    if guess > x:
        print "Lower"
        running = True

raw_input("Press enter to continue....")

Everytime I execute it the answer is 99, how do I reset the variable everytime I execute the program. Also do I need that while loop even though I have the for loop? I just used for loop because I don't know how to store a random integer. Thanks for the help.

Votes + Comments
Look at what a for loop is please.
10 Years
Discussion Span
Last Post by bumsfeld

Hi EriCartman13,

If you want to create a random number between 1 and 100, the conventional way of doing that in Python is:

import random
x = random.randint(1,100)

I would swap this code in and remove the for loop. The resulting program creates a random integer between 1 and 100, assigns that integer to x, then keeps the user guessing until he or she gets the number right.
For loops are not used to generate random numbers - please read the Python documentation for details on the for loop: here.


Yea I know that is not what it is for, but I just didn't know how to make a random integer. Thanks for the help though.


For me the the random integer stays the same after doing something like:

import random
x = random.randint(1, 100)
print(x) //I print x
print(x) //x stays the same

pythondevguy do this:

import random
x = random.randint(1, 100)
x = random.randint(1, 100)

Edited by bumsfeld


Something does not work right with code stuff!
Ah, delayed action!

Edited by bumsfeld

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.