problems with looping, i suppose

John Salerno johnjsal at NOSPAMgmail.com
Mon Mar 27 13:54:03 EST 2006


John Salerno wrote:
> Here's an exercise I was doing 

This might help:

import random

number = random.choice(range(1, 100))
tries = 0

while True:
     try:
     	guess = input('Enter a number between 1 and 100: ')
     	break
     except NameError:
     	print 'Invalid number\n'
     	continue

while True:
	tries += 1
	try:
     	if guess == number:
     		print 'Congratulations! You got it in', tries, 'guess(es).'
     		break
     	elif guess < number:
     		guess = input('Too low. Try again: ')
     	elif guess > number:
     		guess = input('Too high. Try again: ')
     except NameError:
     	print 'Invalid number\n'
     	continue

raw_input()



More information about the Python-list mailing list