473,799 Members | 3,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 7246
I tried this code and it worked fine:

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

Feb 23 '06 #2
da********@gmai l.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********@gmai l.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********@gmai l.com wrote in news:1140725159 .143882.202630
@j33g2000cwa.go oglegroups.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.pytho n, 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

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

Similar topics

33
3192
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
1332
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
6510
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
11
2240
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 onloadListeners, addOnLoadListener below window?
94
30358
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 Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
9
1442
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 change it to:
0
2198
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 problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
21
1896
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 made: (p. 39: "The ref modifier is essential in implementing a swap method") This is untrue. The following program demonstrates it. See how method "func4Swap" does a swap of two int variables without using ref, but using a 'helper' class and...
4
7123
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 operand shall be an lvalue or a qualified-id". His expression was &Test("test2"); IMHO, the compiler generated a warning because it was being laxist.
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.