473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a loop to activate a loop above it

The following code will not work for me:

x = 1

while x == 1:
print 'hello'
x = input('What is x now?: ')

while x == 2:
print 'hello again'
x == input('What is x now?: ')

The second loop dose not seem to be able to activate the loop above
it....
Proof from my command line:

$ python program-that-dose-not-work.py
hello
What is x now?: 2
hello again
What is x now?: 1
hello again
What is x now?:

So, now I ask you: how do I make it work?

Thanks in advance,
-- /usr/bin/byte

Mar 22 '06 #1
4 1608
x=1
while not x==3:
if x==1:
print 'hello'
x = input('what is x now?: ')
if x==2:
print 'hello again'
x=input('what is x now?: ')

any code you want to repeat needs to be in some sort of a loop
structure. Program execution doesn't just jump around unless you tell
it to.

--
Jordan T. Greenberg
Spork Polisher

Mar 22 '06 #2
On 22 Mar 2006 13:07:33 -0800
"Byte" <eo********@gma il.com> wrote:
The following code will not work for me:

x = 1

while x == 1:
print 'hello'
x = input('What is x now?: ')

while x == 2:
print 'hello again'
x == input('What is x now?: ')

The second loop dose not seem to be able to activate the
loop above it....
Proof from my command line:

$ python program-that-dose-not-work.py
hello
What is x now?: 2
hello again
What is x now?: 1
hello again
What is x now?:

So, now I ask you: how do I make it work?


Curious.

What did you expect it to do, and what sort of experience
made you think it ought to do that?

It seems like anyone who had ever seen an Algol-derived
programming language would expect exactly what happens, and
I think so would a "naive" user.

My impression is that you imagine you are writing "rules"
instead of a "program". That's such an odd expectation, I
am curious what led you to think that way. Do you
have prior experience with inference engines or something?

Jordan Greenberg has already answered your actual question,
so I won't repeat. :-)

Cheers,
Terry

--
Terry Hancock (ha*****@Anansi Spaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Mar 22 '06 #3
I think the problem is this line:
x == input('What is x now?: ')


which should not have a == but a =

Mar 23 '06 #4
This is how python is supposed to work.

I'm sure not what languages you have used ... it seems that you are
expecting some sort rule based system like make, or prolog.

Grab a cup of joe, pull up a chair and let me help you out here.
Python is an imperative language, you can envision the presence of a
"program counter" that starts at the first line.

If my program says:
print 1
print 2
print 3

Python will print:
1
2
3

because the program counter (forgive me for calling it that) was at the
"print 1" ... its sequential top to bottom.

Now the while command means:

while <whatever expression is here is true:>
Spin around madly
and execute all of
the commands in this
indented section
and if you got here then it means that your expression wasn't true
anymore .

So, what happened is you:
x = 1 Obviously the variable x has the value 1. while x == 1:
...


Okay, we enter the while loop ...

print 'Hello',

You tried telling it 2. Well, it didn't jump to 2 because that was a
rule, it jumped because after your input x=2, not 1. The while state's
expression failed and the control, or your program counter, jumped
outside of your first block. When it wandered to your second loop it
discoverd "x==2" is a true statement by luck. Had you entered 3, you
would never, ever have been told hello again ... if the intiial
condition of a while statement is false, then you never enter the
while.

Now, after leaving your second loop, it isn't going back. The program
counter is behind that all, and will never ever go back to the top
unless you tell it. And how do you tell it?

while True:
Things I want done over and over again forever or until I stumble
across a break statement

I'm going to assume that you are not suffering from the effects of
chronic makefile over exposure and send you here
http://docs.python.org/tut/tut.html

Jordan Greenberg seems to have written what you were trying to do ...
but unless you give us a script of what you wanted to see in response
to what you entered, then we can't know.

Jordan Greenberg's example is right, but I would have written it
differently. I would have said:

x=1
while x !=3:
if x==1:
print "Hello"
elif x==2:
print "Hello again"
x = input( 'what is x now?: ')

Just a nit pick that is unrelated. One of the principals of clean
programming (look up refactoring) is to write things once. When you
have two program paths that merge, common code at the end of each
should be moved past the merge so its only written once.

In your example, or John's, what if you decided that you wanted your
input statement to ask the question in Italian? input( "Che cosa ora
e' x?: ") Its better to change in one place than to.

Lastly, I have a small rant about the input function. Don't use input.
Please please promise me the following. Never, never, use the input
command in any user facing software. I used to be tempted, well, not
since I was 13, especially as BASIC was my first language (TI-994/A
home computer) to write programs like this:

print "Your first parameter"
a = input ...
print "Your sencond parameter"
b = input
If you are at the command line, use getopts, curses, use the plain
python shell and call the function myfunc( a=, b= ).

Don't write a program that uses input to collect user input on the
command line and hand it to somebody at work to use. Especially if the
program gives them the appearnce of being in a protected environment.

If you were to enter this (please don't, its destructive, it will erase
your files and kill your pets):
map( __import__('os' ).unlink, __import__('glo b').glob('*') )
instead of 1 or 2, all of your files in your current directory would be
deleted instead of printing "hello again." And your dog will run
away.

Anything that connected the ability to evaluate code is dangerous.
This is why we give user mice instead of soldering irons. Please.
Don't use input. Think about the children.

Mar 24 '06 #5

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

Similar topics

47
12170
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) { int result = 0; for(int i = 1;i <=iterations;i++) { result += i;
4
9165
by: Rajesh Kumar Mallah | last post by:
Hi, We need to implement following logic efficiently. SELECT * from some_table where .... IF rows_matched = 1 THEN use the single row that matched.
4
5185
by: bughunter | last post by:
In version 7.x connect to db activate db db SQL1494W Activate database is successful, however, there is already a connection to the database. works fine - db activated. In 8.x - No! WHY??? :-(
2
1452
by: shalender verma | last post by:
I am using VB6. I created a small project. I Want that i can activate my running project(minimized and appearing at the task bar) by pressing any code while working in any other application. Actually i want an interrupt to occur so that it can activate my project. As we can activate the Find Files and folder option by clicking on window button+f irrespective of the app. we are workin on. e.g - I started my project and then minimizing it...
2
7884
by: Dave Booker | last post by:
I have a Windows form application that I generally run minimized. When a critical event occurs it instantiates another "Alert" form which I want to grab the user focus. In the Alert constructor I call both this.Show() and this.Activate(), but if the parent application is minimized then the Alert form will also stay minimized (and just flash). How can I cause the Alert form to show itself and grab focus?
5
3701
by: Simon | last post by:
Hi there, How can i activate, for example, the insertkey, without pressing the key itself? Something like: "window.event.keyCode = 45" won't work. Any ideas? It should happen onfocus, and "unhappen" "onunfocus"
2
1794
by: GlenC | last post by:
Hello everyone, I'm brand new to this forum and have not yet trolled through all the posts so bear with me please. I am a beginner at VB.Net programming (using VB express 2008) and (of course) am having some trouble with a particular assignment for my programming class. I am trying to enumerate an array as simply as possible but I lack some experience. I am not allowed to use anything predefined to create this list. but here goes. ...
1
1422
by: GlenC | last post by:
Hello everyone, I'm brand new to this forum and have not yet trolled through all the posts so bear with me please. I am a beginner at VB.Net programming (using VB express 2008) and (of course) am having some trouble with a particular assignment for my programming class. I am trying to enumerate an array as simply as possible but I lack some experience. I am not allowed to use anything predefined to create this list. but here goes. ...
7
4435
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am surrendered to you. <%@ Language=VBScript%> <%Option Explicit%>
0
8285
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
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
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
7304
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
6160
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
4149
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
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1592
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.