"Temporary" Variable

2006-02-22 Thread darthbob88
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. If this is in the FAQ,
my apologies, I did not find it. Thank you in advance.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Temporary" Variable

2006-02-23 Thread darthbob88

Steven D'Aprano wrote:
> [EMAIL PROTECTED] 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 "", 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Temporary" Variable

2006-02-24 Thread darthbob88
Reply to all: I realize that naming a variable "spam" is not entirely
kosherized. It was originally named secret, but I renamed it in a fit
of whimsy. The language is named after Monty Python's Flying Circus, is
it not? Remember the Spam Sketch.
http://en.wikipedia.org/wiki/Spam_sketch
I thank you for finding the problem; I thought it might involve string
v int comparisons when Comrade D'Eprano's code went wonky. Many thanks,
and good luck to you.
PS: As for the inappropriate naming, the Swedes tried to sell their
vacuums under the motto "Nothing sucks like an Electrolux."

-- 
http://mail.python.org/mailman/listinfo/python-list