954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do you reset your variable everytime?

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.

EriCartman13
Junior Poster
114 posts since Apr 2005
Reputation Points: 10
Solved Threads: 2
 

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 arenot used to generate random numbers - please read the Python documentation for details on the for loop: here .

G-Do
Junior Poster
147 posts since Jun 2005
Reputation Points: 41
Solved Threads: 31
 

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.

EriCartman13
Junior Poster
114 posts since Apr 2005
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You