"Temporary" Variable

D

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.
 
M

markscala

I tried this code and it worked fine:

while 1:
var1 = raw_input("Enter a number: ")
print "You entered:",var1
var1 = int(var1) + 1
print var1
 
S

Steven D'Aprano

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.
.... var1 = raw_input
.... test var1
File "<stdin>", line 3
test var1
^
SyntaxError: invalid syntax

How about posting your actual code?

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."
 
D

darthbob88

Steven said:
Hmmm. I get a syntax error.

... 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"
 
R

Rick Zantow

(e-mail address removed) wrote in @j33g2000cwa.googlegroups.com:
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"

Raw_input isn't giving you what you think it is. You're comparing it to
an integer, not a string. Does that help?
 
S

Steven D'Aprano

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


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.
 
J

Jeffrey Schwab

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

My comments inserted inline.





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

Lighten up. This isn't the middle of a 100-KLOC corporate monstrosity,
it's a 1/2-page usenet post.
 
S

Steven D'Aprano

Lighten up. This isn't the middle of a 100-KLOC corporate monstrosity,
it's a 1/2-page usenet post.

The original poster is also a newbie who was having trouble with the
difference between strings and ints. If Guido called a variable spam,
I wouldn't presume to correct him. When Newbie McNew does it, it might
very well be because he doesn't know any better.

Just out of curiosity, when do you think is the right time to begin
teaching programmers good practice from bad? Before or after they've
learnt bad habits?
 
B

bonono

Steven said:
Just out of curiosity, when do you think is the right time to begin
teaching programmers good practice from bad? Before or after they've
learnt bad habits?
When you have authority over the coding guideline. Naming things is not
something limited to programming and most people know the importance of
choosing the appropriate ones. If on the other hand some names have
been chosen that have actual side effect(in python program), like
builtin function names, it is appropriate to point that out, though I
don't see that case here.
 
S

Steven D'Aprano

When you have authority over the coding guideline.

By that logic, only senators and other government ministers are allowed
to tell people "Committing murder is strongly discouraged, don't do it",
because only they have authority over what is made law and what is not.

I don't need to be Guido van Rossum to know that calling a dict
"myFloat" is a terrible idea. Your suggestion that one needs somehow to be
authorized (by whom?) before you are allowed to point out poor programming
practice not only goes against the entire community ethos of
comp.lang.python, but is offensive in the extreme.

Naming things is not
something limited to programming and most people know the importance of
choosing the appropriate ones.

"Most people". Let's not mention the Chevy Nova ("it won't go") then,
will we?

Car manufacturers seem to have a talent for not just choosing
bad names, but for getting them horribly, horribly wrong. I suggest
you google on "Opel Ascona", "Buick LaCrosse", "Honda Fitta", "Mitsubishi
Pajero", and the "Mazda LaPuta", to discover just why those cars went over
like lead balloons in places like Spain, Finland and Quebec.

But I digress. What of those that don't know the importance of choosing
appropriate names? We should just let them shoot themselves in the foot,
even when it is obvious to Blind Freddy that they are struggling with some
really basic concepts, and that they probably don't even know the gun is
loaded?

If on the other hand some names have
been chosen that have actual side effect(in python program), like
builtin function names, it is appropriate to point that out,

By your earlier logic, that should only be permitted to those who have
authority to create builtins, certainly not the likes of you and me.
 
J

Jeffrey Schwab

Steven said:
The original poster is also a newbie who was having trouble with the
difference between strings and ints. If Guido called a variable spam,
I wouldn't presume to correct him. When Newbie McNew does it, it might
very well be because he doesn't know any better.

Sorry if I snapped at you. But you didn't "correct" him, as he what he
did wasn't wrong. You just talked down to him.
Just out of curiosity, when do you think is the right time to begin
teaching programmers good practice from bad? Before or after they've
learnt bad habits?

I'm not convinced the OP has a "bad habit." Frankly, I prefer postings
that have a sense of humor. I wouldn't want to see this turned into a
purely techical forum.

import random
print ", ".join(['spam' for i in range(random.randint(1, 9))] +
['bacon', 'eggs', 'and']), 'spam'
 
D

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."
 
S

Steven D'Aprano

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

Absolutely!

And it is a fine thing to use Monty Python references as generic variables
in Python, instead of foo, bar, baz and so forth.

All up, I'm surprised at the fuss made over a throw-away line.
I thank you for finding the problem; I thought it might involve string
v int comparisons when Comrade D'Eprano's code went wonky.

My 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."

Which is a wonderful, deliberately ironic advertising line. I love it!!!
 
T

Terry Hancock

On 24 Feb 2006 14:08:22 -0800
Reply to all: I realize that naming a variable "spam" is
not entirely kosherized.

In fact this is untrue: It is an official rule straight
from the BDFL himself that example programs should contain
words like "spam", "ham", "eggs" from the spam sketch.
Other Monty Python sketch variables are acceptable as well.

This in contrast to longstanding programmer practice to use
the names "foo" and "bar" etc.

Ironically, in introducing Python 2.5 features in his key
note, he consistently used "foo" and "bar" himself.

My faith is so shaken. ;-)

Cheers,
Terry Hancock

--
Terry Hancock ([email protected])
Anansi Spaceworks http://www.AnansiSpaceworks.com
 
D

Dennis Lee Bieber

Ironically, in introducing Python 2.5 features in his key
note, he consistently used "foo" and "bar" himself.
At least it was "foo"... and not "fu".... After all, "fubar" is more
apropos to PERL
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top