473,396 Members | 1,789 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,396 software developers and data experts.

breaking a loop

Hi, I'm just learning Python....thanks in advance...

Do you get out of this loop?

Problem: When I type 'exit' (no quotes) the program doesn't quit the loop...it actually attemps to find entries that containt the 'exit' string.

Desired behavior: when I type 'exit' the program should quit.

def search():
searchWhat = ""
while searchWhat != 'exit':
searchWhat = "%%%s%%" % raw_input ('Search for : ')
cursor.execute("select * from TABLE where FIELD like %s", (searchWhat))
result = cursor.fetchall()
print '+------------------------------------------------------------------+'
print '| # | Name | LastName |'
print '+------------------------------------------------------------------+'
for record in result:
print ' ', record[0], ' : ', record[1], ' ==> ', record[2]
print '+------------------------------------------------------------------+'
#end for statement, end of search
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Aug 11 '05 #1
3 1556
What you do is asking for a string, and then embed the result in '%'
characters, as this is what you do in the sql statement (btw: where is
the cursor defined?)

It is probably a better idea to construct the sql statement with the
direct result of raw_input, instead of format it straight away - see
below for the idea

martin@ubuntu:~ $ python
Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
py> s = raw_input('Prompt: ')
Prompt: exit
py> print s
exit
py> if s == 'exit':
.... print 'yes'
....
yes
py> t = '%%%s%%' %s
py> print t
%exit%
py> sql = "select * from TABLE where FIELD like %s", (s)
py> print sql
('select * from TABLE where FIELD like %s', 'exit')
py> sql = "select * from TABLE where FIELD like '%%%s%%" % s
py> print sql
select * from TABLE where FIELD like '%exit%
py>

Aug 11 '05 #2
> Desired behavior: when I type 'exit' the program should quit.

def search():
user_input = ''
while True:
user_input = raw_input('Search for: ')
if user_input == 'exit': break
print 'processing', user_input

Note that putting user input directly into SQL statements leaves your
database open to SQL-injection attacks :-)

Aug 11 '05 #3
I think u need break before exit()
so if u want break from any loop just add break

"el chupacabra" <no*******@noway.com> wrote in message
news:fU****************@tornado.socal.rr.com...
Hi, I'm just learning Python....thanks in advance...

Do you get out of this loop?

Problem: When I type 'exit' (no quotes) the program doesn't quit the loop...it actually attemps to find entries that containt the 'exit' string.
Desired behavior: when I type 'exit' the program should quit.

def search():
searchWhat = ""
while searchWhat != 'exit':
searchWhat = "%%%s%%" % raw_input ('Search for : ')
cursor.execute("select * from TABLE where FIELD like %s", (searchWhat)) result = cursor.fetchall()
print '+------------------------------------------------------------------+' print '| # | Name | LastName |' print '+------------------------------------------------------------------+' for record in result:
print ' ', record[0], ' : ', record[1], ' ==> ', record[2] print '+------------------------------------------------------------------+' #end for statement, end of search
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Aug 13 '05 #4

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

Similar topics

2
by: Jay Davis | last post by:
We have a lot of simple code using 'urllib', that basically just goes out and opens and read()'s some http url. Very basic, ten line scripts, and they work fine with urllib. We want to use...
9
by: rbt | last post by:
What is the appropriate way to break out of this while loop if the for loop finds a match? while 1: for x in xrange(len(group)): try: mix = random.sample(group, x) make_string = ''.join(mix)...
5
by: Uday Deo | last post by:
Hi everyone, I am looping through 4 nested loops and I would like to break in the inner most loop on certain condition and get the control on the 2 nd loop instead of 3rd loop. Here is briefly...
22
by: mihiri | last post by:
When I use this code it compile without errors ,but when run it ,it shows segmentation fault.I have general idea about segmentation fault,but I can't identify why it happen in this code.Eventhough I...
13
by: Sunbags | last post by:
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the...
9
by: nunzio | last post by:
Hi, my problem is apparently simple: I have a while loop controlled by a flag and I want to interrupt it by clicking on a stop button. void buttonStart_Click( Object * sender, System::EventArgs *...
6
by: noemailplease0001 | last post by:
Any elegant way of breaking out of the outer for loop than below, I seem to have come across something, but it escapes me for i in outerLoop: for j in innerLoop: if condition: break else:...
5
by: Kris Kowal | last post by:
I had a thought that might be pepworthy. Might we be able to break outer loops using an iter-instance specific StopIteration type? This is the desired, if not desirable, syntax:: import...
13
by: Andrew | last post by:
Hi I was wondering if there is anyway with XML RPC to send a string of text from the server to the client with out calling return thus breaking my loop for example def somefunc(): for...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.