"Temporary" Variable

darthbob88 at gmail.com darthbob88 at gmail.com
Thu Feb 23 15:05:59 EST 2006


Steven D'Aprano wrote:
> darthbob88 at gmail.com wrote:
>
> > Problem: I wish to run an infinite loop and initialize a variable on
> > each iteration. Sort of like, "Enter Data", test it, "No good!", "Next
> > Try?", test it, etc. What I've tried is simply while 1: var1 =
> > raw_input, test var1, then run through the loop again. What results is
> > var1 gets and keeps the first value it receives.
>
> Hmmm. I get a syntax error.
>
>  >>> while 1:
> ...     var1 = raw_input
> ...     test var1
>    File "<stdin>", line 3
>      test var1
>              ^
> SyntaxError: invalid syntax
>
> How about posting your actual code?
Soitenly.
#!/usr/bin/python
#simple guessing game, with numbers
import random
spam = random.randint(1, 100)
print spam #debugging purposes
while 1:
	guess = raw_input("What's your guess, friend? ")
	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"
> -- 
> Steven.




More information about the Python-list mailing list