473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

"Temporary" Variable

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.

Feb 23 '06 #1
13 7204
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

Feb 23 '06 #2
da********@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?

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

--
Steven.

Feb 23 '06 #3

Steven D'Aprano wrote:
da********@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.


Feb 23 '06 #4
da********@gmail.com wrote in news:1140725159.143882.202630
@j33g2000cwa.googlegroups.com:

Steven D'Aprano wrote:
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"


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

Feb 23 '06 #5
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.

Feb 23 '06 #6
Steven D'Aprano wrote:
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.


Lighten up. This isn't the middle of a 100-KLOC corporate monstrosity,
it's a 1/2-page usenet post.
Feb 24 '06 #7
On Fri, 24 Feb 2006 00:24:25 +0000, Jeffrey Schwab wrote:
Steven D'Aprano wrote:
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.


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?

--
Steven.

Feb 24 '06 #8

Steven D'Aprano wrote:
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.

Feb 24 '06 #9
On Fri, 24 Feb 2006 01:50:40 -0800, bonono wrote:
Steven D'Aprano wrote:
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.


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

Feb 24 '06 #10
Steven D'Aprano wrote:
On Fri, 24 Feb 2006 00:24:25 +0000, Jeffrey Schwab wrote:

Steven D'Aprano wrote:
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.
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.


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'
Feb 24 '06 #11
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."

Feb 24 '06 #12
On Fri, 24 Feb 2006 14:08:22 -0800, darthbob88 wrote:
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!!!
--
Steven.

Feb 26 '06 #13
On 24 Feb 2006 14:08:22 -0800
da********@gmail.com wrote:
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 (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Feb 26 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
7
by: Jamie Julius | last post by:
Consider the following struct: struct TestStruct { public int a, b, c; public TestStruct(int a, int b, int c) { this.a = a;
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
11
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
9
by: Jameson.Quinn | last post by:
I have: try: for line in open(myFileName): count += 1 except IOError: print "Can't open myfile" (I know, this is bad, I never close the file, but its just for illustration). But then I...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
4
by: James Kanze | last post by:
On Nov 18, 5:50 pm, Pete Becker <p...@versatilecoding.comwrote: Has this changed in the latest draft. According to my copy of the standard (version 1998---out of date, I know), "The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.