RE: Python Challenge #2: Guessing The Number 12-16-2015, 11:42 PM
#3
(12-16-2015, 11:31 PM)m0dem Wrote: I'm not really a code golf kind of guy, so I don't usually shoot for the smallest code.
Thanks for the challenge!
Code:import random def main(): gLeft = 8 num = random.randint(1, 100) print("Guess a number from 1 to 100.") while gLeft > 0: gLeft -= 1 guess = int(raw_input("> ")) if guess > num: print("Too high.") elif guess < num: print("Too low.") elif guess == num: print("You guessed the right number in {} tries!".format(8 - gLeft)) return print("All your tries are gone.")
*ADVANCED VERSION*
Code:import random def inRange(num, low, high): if num < low or num > high: return False return True def mainA(): low = 1 high = 100 print("Choose a random number between {} and {}.".format(low, high)) num = low - 1 while not inRange(num, low, high): num = int(raw_input("> ")) gLeft = 8 cRange = [1, 100] print("Computer, guess a number from 1 to 100.") while gLeft > 0: gLeft -= 1 # computer algorithim guess = int((cRange[1] - cRange[0]) / 2) + cRange[0] print("> {}".format(guess)) if guess > num: print("Too high.") cRange[1] = guess elif guess < num: print("Too low.") cRange[0] = guess elif guess == num: print("The computer guessed our number in {} tries!".format(8 - gLeft)) return print("The computer failed to guess the correct number!")
Nice. Haven't looked at your second code since I still haven't done it

But, for your first one you should have set it so the user can play an unlimited amount of times rather than setting it to 8 games. Maybe I was a bit unclear or you just forgot. All good.






![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)