djtravz djtravz - 1 year ago 46
Python Question

randint() issue with Python number guessing game

The following is a snippet of code that is supposed to make a higher lower game. It is supposed to get a random number between two variables. But it doesn't go in between the 2 variables. Just try it and you will see.

from random import randint
high = 1000
low = 1
guess_num = 5
def me_guess(guess_num,high,low):
print " "
guess_num = randint(low,high)
guess_num = str(guess_num)
fact = raw_input(guess_num + " (higher or lower or yes) ")
if fact == "higher":
guess_num = low
me_guess(guess_num,high,low)
elif fact == "lower":
guess_num = high
me_guess(guess_num,high,low)
elif fact == "yes":
print "Yay! I got it."
me_finish(guess_num)
else:
print "Error. Guessing again."
me_guess(guess_num,high,low)
me_guess(guess_num,high,low)

Answer Source

You want to change the "low" not "guess_num" ,the same for "max" in the if else statments...

guess_num = low should be low = guess_num

also don't str() guess_num

from random import randint
high = 1000
low = 1
guess_num = 5
def me_guess(guess_num,high,low):
  print " "
  guess_num = randint(low,high)

  fact = raw_input(str(guess_num) + " (higher or lower or yes) ")
  if fact == "higher":
    low = guess_num
    me_guess(guess_num,high,low)
  elif fact == "lower":
    high = guess_num
    me_guess(guess_num,high,low)
  elif fact == "yes":
    print "Yay! I got it."
    me_finish(guess_num)
  else:
    print "Error. Guessing again."
    me_guess(guess_num,high,low)

me_guess(guess_num,high,low)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download