I see...I was apparently doing something wrong because my 'for' loop kept throwing an 'index out of range' error for me. Thanks, that works the way I need it to and point taken on the variables! :)

I'm trying to use this code for a fibonocci sequence where I'm setting the starting numbers
as b= and c=. What I'd like to do though is have it stop after so many numbers are added together, in this case it would be 53rd (a=53). I've tried a 'for i in range(53):'loop but
that doesn't work for this. The a,b,c numbers can be randomly choosen, which I
have working, but for this snippet I'm setting them as static. Suggestions?

a =  53
b = 7
c = 18

k, l = b, c
while l < 9999999999999999:
    xa = l
    k,l = l, k+l
    print xa,

   #for i in range(a):
         #print i

    #for x in xa: ??

Thanks all, for the suggestions!

Hi,

If I have a sentence like this:

"I now have a total of % yellow bananas!"

but I need it to change the number 2 to a 3, print, then a 4, print, etc until it reaches a set number like 10, how can I go about that?

sentence = "I now have a total of % yellow bananas!"

for i in range(2,11):
    print sentence %i

but that gets me an error:
ValueError: unsupported format character 'y' (0x79) at index 9

When I try it with a different sentence:

sentence = " I have % dogs!"
for i in range(2,11):
    print sentence %i

...it almost works:
I have 2ogs!
I have 3ogs!
I have 4ogs!
I have 5ogs!
I have 6ogs!
I have 7ogs!
I have 8ogs!
I have 9ogs!
I have 10ogs!

Or am I going about this totally the wrong way? Any suggestions? Thanks

I'm having trouble getting my python scripts to insert into a mysql database. I'm able to do other commands successfully such as adding or dropping tables, selecting items from tables but inserting into tables isn't working. I can manually insert but the script won't do it.
I have an entry field for a last name and am using .get() to grab the data. If I enter the name 'Jones' into the field and submit it, I get this error:
OperationalError: (1054, "Unknown column 'Jones' in 'field list'")

If I move the quotes around I won't get an error message but it also won't show up in the database.

Manually:

[code]
mysql>INSERT INTO persons (lname) VALUES ("Jones");
Query OK, 1 row affected (0.36 sec)
[/code]

Script portion:
[code]
test4=win4a.get() # Last name
cursor.execute("INSERT INTO persons (lname) VALUE (%s)"% (test4))
[/code]

If tried this which gets no errors but also doesn't insert:
[code]
cursor.execute("INSERT INTO persons (lname) VALUE ('%s')"% (test4))
[/code]

Even if I change it to the following, I will still get the same error message:
[code]
cursor.execute("INSERT INTO persons (lname) VALUE ('Jones')")
[/code]

My script is using the right database & tables.

Ok, thank you for the reply. I will start a new database with at least 5 tables (more appropriately named). Would it make sense to add a foreign key to the sales items bought, to point to the customer table? I'm trying to read up on it.

Sorry about the 'code' tags.....couldn't edit the post.

Sorry about the 'code' tags.....couldn't edit the post.

I've been working on a project lately that is written in python and I want to incorporate mysql into it. The python code is basically a sales report writing system in which every sales report will have it's own number such as 10-0001, 10-0002 and will contain customer information like first & last names, addresses, phone numbers, etc. There is also a page, if needed, for items such as televisions, brand names of items, color of items, serial numbers of items, values of items, etc, and there will also be a narrative page where several paragraphs of info can be entered.
I originally made a database named "maindb" which has a table named "info". Inside of the table I made a few columns such as "fname", "lname", "City", "State", "Brand","Item". I now want to go through my entire code and set up all of the entries fields with mysql but I'm not sure if I should make it so every report number creates a new table with new columns, or if I should just stick with one table and a whole lot of columns in it.

Info to be entered can include:

[code]
The employee
sales report number
date & time of sale

customer's first & last name
customer's address
customer's phone numbers (cell, work, home)
type of payment (cash, credit card, check)

number of items
item description
make of item
model of item
serial number of item
price of item

sales narratives/notes

[/code]

I read about 'normalization' which seems ...

I've been trying those suggestions but still can't get it to work. Is this what you meant by putting SaveThis at the end of SaveData and changing it's name? Can you give me some examples of declaring it globally?

[CODE]
def SaveData():
f = a.get()
g = b.get()
...code...

def save_data(cursor):
    h = c.get()
    j = d.get
    ...code...

[/CODE]

Hi, I've been using tkinter to write a program that has a root window and many toplevel windows. I was able to create them successfully but my next step was to incorporate mysql with it so it would actually store any data that was entered in the various entry fields. I started with a typical script to connect to mysql with the username and password hardcoded and was able to have data saved into the database. My problem now is that I want a login window where a user will enter their own username and password and if correct, it will connect to mysql. From there, toplevel windows can be opened and data from entry fields stored. The code below is a mock up of what I have (up over 1300 lines now) and it will do the login with valid username/pass but now the other sql commands won't execute because I guess the 'global name cursor' isn't defined, or is out of the loop? How can I correct this?

[CODE]
from Tkinter import *
import sys
import MySQLdb

Toplevel window that opens when "NEXT" button is clicked in "NextWindow" widget

def NewWindow():
win = Toplevel()

# entry boxes put in a frame, text stored in variables k, l
ent_frame = Frame(win)
Label(ent_frame, text="ITEM:").pack(side=LEFT)
k = Entry(ent_frame, width=15)
k.pack(side=LEFT)
Label(ent_frame, text="BRAND:").pack(side=LEFT)
l = Entry(ent_frame, width=15)
l.pack(side=LEFT)
ent_frame.pack()

# using get method for entry boxes above
def SaveIt():
    m = k.get()
    n = l.get()
    # executing a sql command and ...

Wow :) very impressive and exactly what I was looking for. Thanks!

I have an array of numbers and want to print out how many of each number there are in the array. My method works but I was wondering what a more efficient way of doing this would be. I figure a loop would work but I haven't figured out how to do it with this:

[code]
s='1234444432187667890000568984'
print 'I found %i:' % s.count('0'),0
print 'I found %i:' % s.count('1'),1
print 'I found %i:' % s.count('2'),2
print 'I found %i:' % s.count('3'),3
print 'I found %i:' % s.count('4'),4
print 'I found %i:' % s.count('5'),5
print 'I found %i:' % s.count('6'),6
print 'I found %i:' % s.count('7'),7
print 'I found %i:' % s.count('8'),8
print 'I found %i:' % s.count('9'),9
[/code]

I did find this snippet which is fast in counting but doesn't print out like I want it to:

[code]
s='1234444432187667890000568984'
ans=dict((i,s.count(i)) for i in set(s))
print ans
[/code]

Thank you guys for your replies!

Hi, I'm trying to run code that asks a yes or no question. If a yes or no isn't entered, then the question will continue to re-ask say every 3 seconds until a yes or no is entered. Here's the basics of it:

[code]
while True:
response = raw_input(" Are you ready? ")
if response.lower() == "yes":
print ("Then lets get started!")
break
elif response.lower()=="no":
print ("Please try again later....")
break
else:
print ("Please enter a 'yes' or 'no' response!")
[/code]

With that, I can add another while True: after the last 'else' statement but that's not exactly what I'm after. Instead, I'd like the first question "Are you ready?" to keep repeating until 'yes' or 'no' is entered. Can I somehow incorporate sleep() in this and how can I get the raw_input question to keep repeating? Example output:

Are you ready? maybe
Are you ready?
(3 second delay, if no answer ask again)
Are you ready?
(3 second delay, if no answer ask again)
Are you ready?
(3 second delay, if no answer ask again)
Are you ready?yes
"Then lets get started!"

jlm699, thank you! That helped alot!

[QUOTE=slate;873186]This is not true, or you did not show us that. Your code does not produce the desired output, and it is not, that it has numbers instead of characters.

I think the problem:

is solved.

Please open a new topic with something similar:
help with simple discrete math homework.

I will gladly help.[/QUOTE]
I put a loop up of numbers to show that I can run a loop with a given amount of chars. I can add 2 more values to it if a list of 5 is what I'm after. Running the numbers loop will produce:
0 0 0
0 0 1
0 0 2
and so on. After the suggested use of chr() I was able to write the loop to use letters instead of numbers but my original question was how to do this with uppers as well as lowers:
[quote]
How can I write a loop using letters instead of numbers so it looks like this:
2 sets used 'a-z' and 'A-Z':
[/quote]
At your suggestion, I will mark this as solved and start a new post.

Thanks for the reply. Here's part of my problem with that:
[code]
for i in range(97,122):
for j in range(97,122):
for k in range(97,122):
print chr(i),chr(j),chr(k)
[/code]
That type of loop will work but will take too long to run through the combos and I would also need the Caps (65,90) to run through it.

How can I write a loop using letters instead of numbers so it looks like this:
2 sets used 'a-z' and 'A-Z'
aaaaa
aaaaA
aaaAa
aaAaa
and so on:
aaaAA
then on to b:
bbbbb
bbbbA

I can do it with numbers, something like this:
[code]
for i in range(10):
for j in range(10):
for k in range(10):
print i,j,k
[/code]

How does this work with letters?

Yes it worked....I did finally figure it out.

:) I think I figured it out:

[code]
y = 0
for x in range(width):
if pixel_dict[(x,y)][2] > 0:
print( "(%d, %d): %s"% (x, y, pixel_dict[(x, y)]) )
[/code]

When I run this:
[code]
from PIL import Image

img = Image.open('10x.bmp')
for pixel in img.getdata():
if pixel[2] > 0:
print pixel
[/code]
it works but doesn't have the x,y positions. When I try sneelula's code it works but I need to narrow down the list. So...things I've tried:
[code]
for y in range(height):

columns
for x in range(width):
    for pixel in pixel_list:
        if pixel[2] > 0:
          pixel_dict[(x, y)] = pixel_list[ix]
          #print x, y, ix
          ix += 1

[/code]
pixel_dict[(x, y)] = pixel_list[ix]
IndexError: list index out of range
or changing pixel_list to pixel:
[code]
for pixel in img.getdata():
if pixel[2] > 0:
print pixel

create a (x,y):(r,g,b) dictionary
where (x,y) are the location coordinates an image point
and (r,g,b) is the value of the pixel at that point

pixel_dict = {}
ix = 0

rows

for y in range(height):

columns
for x in range(width):
    pixel_dict[(x, y)] = pixel[ix]
    #print x, y, ix
    ix += 1

[/code]
pixel_dict[(x, y)] = pixel[ix]
IndexError: tuple index out of range
or changing this part:
[code]
y = 0
for x in range(width):
for pixel in img.getdata():
if pixel[2] > 0:
print( "(%d, %d): %s"% (x, y, pixel_dict[(x, y)]) )
[/code]
that just prints out a very long list.

I'm bumping this thread to see if I can get a reply....the code by sneekula works for what I need but I can't figure out how to get it to just print out the ones where the 'B' value is greater than 0, which in the image I gave would be this one:
(77, 0): (77, 0, 8)

I guess I would need to add an 'if pixel[2] >0:' statement in and I've tried that in several locations but keep getting error messages.
any help?

I wanted to edit the above thread but anyway.....I got it to loop through so now the last thing I need to work out is how to get it to print out just the values where 'B' is > 0, like in the first code I posted. I'm trying to adjust the code but haven't gotten it yet.

Wait a sec.....after playing around with sneekula's code for a while, I realized that it works, it's just printing out 1 row.
[code]

testing ...
show contents of the first row

y = 0
for x in range(width):
print( "(%d, %d): %s"% (x, y, pixel_dict[(x, y)]) )
[/code]

so I guess I just need a way to make it loop through all rows.
I figured this out by adding:
y += 1 below y=0 and saw that it printed out the 2nd row. What's the way to get it to loop through all rows?

Thanks for the replies. Sneekula, that's along the lines of what I'm after but when I run that on the image I have, it doesn't print out the B values like with the first code I posted.
My code on my image results in:
(partial list)
(77, 0, 8)
(89, 0, 8)
(41, 0, 10)
(50, 0, 7)
(42, 0, 5)
(55, 0, 9)
(112, 0, 5)
(105, 0, 9)
(113, 0, 8)
(97, 0, 6)
Which is just printing out the R,G,B where 'B' is > 0.
but running your code on my image I get:
(partial list)
(0, 0): (0, 0, 0)
(1, 0): (1, 0, 0)
(2, 0): (2, 0, 0)
(3, 0): (3, 0, 0)
(4, 0): (4, 0, 0)
(5, 0): (5, 0, 0)
(6, 0): (6, 0, 0)
(7, 0): (7, 0, 0)
(8, 0): (8, 0, 0)
(9, 0): (9, 0, 0)
(10, 0): (10, 0, 0)

with all B values showing '0' with the exception of one at 'x' position 77 which shows:
(77, 0): (77, 0, 8)

How can I get the list to print out all 'B' values > 0. I tried somehow incorporating mine with yours but I can't get it to work right. Thanks

I'm trying to locate the 'x' positions of certain pixels. Here's and example image:
[url]http://img404.imageshack.us/img404/3418/10x.png[/url]

if I run this code it gives me the 88 'odd' pixels that I'm looking for:
[code]
from PIL import Image

i = Image.open('10x.bmp') # I save it as a bmp
for pixel in i.getdata():
if pixel [2] > 0:
print pixel
[/code]

Now that I have the pixels, I can't figure out how to locate the 'x' positions. How is this done?

Thanks!

I can't seem to figure out what's wrong with this simple code. I have a user input and if it matches prints out 'That's good', if not prints out 'Too bad' but how can I get the input to ignore being case sensitive?

[code]
ans=raw_input("Are you feeling well today? ")
if ans == "yes":
print "That's good"
else:
print "Too bad"
[/code]

I want it so if someone enters "YES" it will still print "That's good"

Works great, thanks!