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.

james.lu.75491856 commented: Look at what a for loop is please. +0

Recommended Answers

All 6 Replies

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

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

python comments use #

pythondevguy do this:

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.