"Temporary" Variable

Steven D'Aprano steve at REMOVETHIScyber.com.au
Thu Feb 23 17:19:39 EST 2006


On Thu, 23 Feb 2006 12:05:59 -0800, darthbob88 wrote:

My comments inserted inline.


> #!/usr/bin/python
> #simple guessing game, with numbers
> import random
> spam = random.randint(1, 100)

It is bad programming practice to give variables uninformative joke names.

How about target instead?

> print spam #debugging purposes
> while 1:
> 	guess = raw_input("What's your guess, friend? ")

Why don't you stick a "print guess" in here to see what your guess is?

(Hint: what is the difference between 1 and '1'?)

> 	if guess == spam:
> 		print "You got it! Nicely done."
> 		break
> 	elif guess < spam:
> 		print "Sorry, too low. Try again."
> 	elif guess > spam:
> 		print "Sorry, too high. Try again."
> 	else:
> 		print "You guessed ", guess



>> You could try this:
>>
>> while 1:
>>      var = raw_input("Give me some data! ")
>>      if var == "some data":
>>          print "Success!"
>>          break
>>      else:
>>          print "No good, try again."
> That works fine with strings and when "some_data" is hardcoded. I run
> into trouble when "some data" is replaced with a number, unquoted. It
> simply says "No good, etc"

Try this, at a command line:

5 == '5'


and see what you get.


-- 
Steven.




More information about the Python-list mailing list