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

Dani AI

Generated

You are always seeing 99 because the for loop walks x from 1 up to 99 and then exits, leaving x set to 99 when the while loop starts. You do not want a for loop here at all; pick the number once, then keep asking until the guess matches. In modern Python, generate it with random.randint(low, high) and store it before the guessing loop. randint(a, b) returns an integer in the inclusive range [a, b]. (docs.python.org)

Also note that in Python 3 the old raw_input() was renamed to input(), and it always returns a string; convert it with int(...) and handle bad input. (docs.python.org)

, printing x twice will show the same value because you did not generate a new one in between. If you want a new random number, call randint again and assign it to x; if you are writing a guessing game, generate x once per game so the player can converge. Unless you explicitly call random.seed(...), Python seeds the generator for you, so you will get a different sequence across program runs by default. (docs.python.org)

Here is a compact, Python 3 version that removes the for, simplifies the loop with elif, and adds basic validation:

import random

def play(low=1, high=100):
    target = random.randint(low, high)
    while True:
        try:
            guess = int(input(f"Enter an integer ({low}-{high}): "))
        except ValueError:
            print("Please enter a valid integer.")
            continue
        if guess < target:
            print("Higher")
        elif guess > target:
            print("Lower")
        else:
            print("Nice!")
            return

while True:
    play()
    if input("Play again? [y/N]: ").strip().lower() != "y":
        break

Security note: for passwords/tokens use secrets (cryptographically strong randomness) rather than random; for games, random is fine. (docs.python.org)

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: .

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.