Re: [Tutor] Is this correct syntax for what I want?
> File "C:\Python24\Account Tracker.py", line 49, in printall >print account,"\t $",accountlist[account]+"\n" > TypeError: unsupported operand type(s) for +: 'float' and 'str' > > So how do I fix this error? What it's saying is you can't add a float and string. If you look at the code you are trying to add "\n" to accountlist[account] which is a float. You need to convert the string to a float or the float to a string. In this case converting the float to a string would be the better approach! :-) However I would personally recommend that you use a format string here since it will give you much better control of the appearance of the output and avoids the need to convert the values. print "%s\t $%0.2f\n" % (account, accountlist[account]) HTH, Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this correct syntax for what I want?
Thanks, it works now perfectly! Thanks for all the help! - Original Message - From: "Alan Gauld" <[EMAIL PROTECTED]> To: "Nathan Pinno" <[EMAIL PROTECTED]>; Sent: Sunday, June 25, 2006 1:07 AM Subject: Re: [Tutor] Is this correct syntax for what I want? >> File "C:\Python24\Account Tracker.py", line 49, in printall >>print account,"\t $",accountlist[account]+"\n" >> TypeError: unsupported operand type(s) for +: 'float' and 'str' >> >> So how do I fix this error? > > What it's saying is you can't add a float and string. If you > look at the code you are trying to add "\n" to accountlist[account] > which is a float. You need to convert the string to a float or > the float to a string. In this case converting the float to a > string would be the better approach! :-) > > However I would personally recommend that you use a > format string here since it will give you much better control > of the appearance of the output and avoids the need to > convert the values. > > print "%s\t $%0.2f\n" % (account, accountlist[account]) > > HTH, > > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this correct syntax for what I want?
When I loaded it up, I got the following error: Traceback (most recent call last): File "C:\Python24\Account Tracker.py", line 82, in ? load_file(accountlist) File "C:\Python24\Account Tracker.py", line 10, in load_file amount = line.next().strip() AttributeError: 'str' object has no attribute 'next' According to it, the 'str' object has no attribute 'next'. So how would I load my file containing my data? Relevant code: def load_file(ac): import os filename = 'accounts.txt' if os.path.exists(filename): store = open(filename, 'r') for line in store: account = line.strip() amount = line.next().strip() ac[account] = amount else: store = open(filename, 'w') store.close Thanks. Nathan Pinno - Original Message - From: "Alan Gauld" <[EMAIL PROTECTED]> To: "Nathan Pinno" <[EMAIL PROTECTED]>; Sent: Sunday, June 25, 2006 1:07 AM Subject: Re: [Tutor] Is this correct syntax for what I want? >> File "C:\Python24\Account Tracker.py", line 49, in printall >>print account,"\t $",accountlist[account]+"\n" >> TypeError: unsupported operand type(s) for +: 'float' and 'str' >> >> So how do I fix this error? > > What it's saying is you can't add a float and string. If you look at the > code you are trying to add "\n" to accountlist[account] which is a float. > You need to convert the string to a float or the float to a string. In > this case converting the float to a string would be the better approach! > :-) > > However I would personally recommend that you use a format string here > since it will give you much better control of the appearance of the output > and avoids the need to convert the values. > > print "%s\t $%0.2f\n" % (account, accountlist[account]) > > HTH, > > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this correct syntax for what I want?
> File "C:\Python24\Account Tracker.py", line 10, in load_file >amount = line.next().strip() > AttributeError: 'str' object has no attribute 'next' > > According to it, the 'str' object has no attribute 'next'. So how > would I load my file containing my data? The str object in question is line. line is a string that you have read from your file. What exactly do you think the next call would return even if line had a next method? I think you may be looking for the split method which will return a list of fields within the string. You will need to convert the string data into whatever format you require. You may find it helpful when debugging these kinds of problems to insert a few print statements, for example a print line at the top of the for loop. It depends a lot on the format of the data in the file. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] beginner: using optional agument in __init__ breaks my code
Hello Group, I am puzzled about this: The following code implements a simple FIFO object. class Queue: "Implementing a FIFO data structure." # Methods def __init__(self): self.queue = [] def emptyP(self): return (self.queue == []) def insert(self, item): self.queue.append(item) def remove(self): if not self.emptyP(): return self.queue.pop(0) def __str__(self): return str(self.queue) This code works as intended. Now my idea is to provide an optional argument to the constructor. So I change it to: def __init__(self, q =[]): self.queue = q Now, something very strange happens: >>> a = Queue() >>> b = Queue() >>> a.insert(12) >>> print b [12] >>> Why do a and b share the same data? "self.queue" is supposed to be an instance variable. What does may change of the __init__ method do here? Thanx for your help. Barb ___ Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] search engine
it would be very hard to know the type of help you need at this time. You have not really written a single line of code and besides, no one will start it for you unless you have a specific problem that needs attention. Again, it sounds to me as though you've not actually grasped the scope of your intended project anyway. Until you have it defined and designed, it will remain/exist in your head which is the same problem that new developers like yourself have. A piece of advice, get a piece of paper, jot down what you want to do, or in effect, design your project prototype and the finally product. This will help you narrow down what you need to do and where/when to start and end. Finally, when you request help from lists like this, make your request very clear and precise; the guys here are more than happy to help you, trust me :) Projects like search engines are too ambiguous, generic and not defined. We don't know if you want to become a new Google, or you want a search facility for your site, or ... Be very direct and please use Google to do your search first before requesting help. Good luck Evans - Original Message - From: vinodh kumar To: tutor@python.org Sent: Saturday, June 24, 2006 7:04 PM Subject: [Tutor] search engine hai all, i am a student of computer science dept. i have planned to design a search engine in python..i am seeking info about how to proceed further. i need some example source code-- ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginner: using optional agument in __init__ breaks my code
On 25 Jun 2006, [EMAIL PROTECTED] wrote: [...] > This code works as intended. Now my idea is to provide > an optional argument to the constructor. So I change > it to: > > def __init__(self, q =[]): > self.queue = q > > Now, something very strange happens: > a = Queue() b = Queue() a.insert(12) print b > [12] > > Why do a and b share the same data? "self.queue" is > supposed to be an instance variable. What does may > change of the __init__ method do here? The values of optional arguments are only once evaluated (when Python reads the definition). If you place there mutable objects like e.g. a list most of the time the effect you see is not what you want. So you have to write it a bit different. def __init__(self, q = None): if not q: q = [] self.queue = q or def __init__(self, q = None): self.queue = q or [] Now you get a fresh list for each instance. Karl -- Please do *not* send copies of replies to me. I read the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] doubt in Regular expressions
hello all, i want to search strings in the database available and return the link of the string instead simply returning the words... by using regular expressions(RE) i got a way to perform the string matchesgive some info regarding how to return the link of the matched strings... ravi. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] doubt in Regular expressions
bump - Original Message - From: ravi sankar To: tutor@python.org Sent: Sunday, June 25, 2006 9:17 PM Subject: [Tutor] doubt in Regular expressions hello all, i want to search strings in the database available and return the link of the string instead simply returning the words... by using regular expressions(RE) i got a way to perform the string matchesgive some info regarding how to return the link of the matched strings... ravi. ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginner: using optional agument in __init__ breaks my code
Barbara Schneider wrote: > Hello Group, I am puzzled about this: The following > code implements a simple FIFO object. > > class Queue: > "Implementing a FIFO data structure." > > # Methods > def __init__(self): > self.queue = [] > > def emptyP(self): > return (self.queue == []) > > def insert(self, item): > self.queue.append(item) > > def remove(self): > if not self.emptyP(): > return self.queue.pop(0) > > def __str__(self): > return str(self.queue) > > This code works as intended. Now my idea is to provide > an optional argument to the constructor. So I change > it to: > > def __init__(self, q =[]): > self.queue = q > > Now, something very strange happens: > > a = Queue() b = Queue() a.insert(12) print b > [12] > > > Why do a and b share the same data? "self.queue" is > supposed to be an instance variable. What does may > change of the __init__ method do here? > When the function definition is processed, Python creates an empty list, which will be used as the default value for q whenever the function is called with no 2nd parameter. Any changes to that list are visible in any call to the function that has no 2nd parameter. To get the behavior I think you want try: def __init__(self, q = None): if not q: q = [] self.queue = q -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginner: using optional agument in __init__ breaks my code
--- Karl Pflästerer <[EMAIL PROTECTED]> schrieb: > > The values of optional arguments are only once > evaluated (when Python > reads the definition). If you place there mutable > objects like e.g. a > list most of the time the effect you see is not what > you want. So you > have to write it a bit different. > > def __init__(self, q = None): > if not q: q = [] > self.queue = q > > or > > def __init__(self, q = None): > self.queue = q or [] > > Now you get a fresh list for each instance. > >Karl Thank you very much. I will use your code as a "recipe", while I still try to understand the mechanism and the reasons behind it. For me this feels odd. Barb ___ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] doubt in Regular expressions
Post Script: Sorry for the double e-mail, Evans. I forgot to forward it to the list the first time. Also, why don't replies automatically forward themselves to the list like the pygame mailing list does? For privacy reasons, in case you want to reply to someone separately? End of P.S. - Evans Anyokwu wrote: > bump Please don't send useless messages like this to the list. If you don't get a reply you want in a few days, send the message again. You waited 30 minutes and bumped the post. This is a huge turn-off for me and makes me not even want to consider your problem. However, I'll assume that you're used to asking questions on high-traffic message boards where this type of thing is necessary. [snip header] > > hello all, > i want to search strings in the database available and return > the link of the string instead simply returning the words... by > using regular expressions(RE) i got a way to perform the string > matchesgive some info regarding how to return the link of the > matched strings... > ravi. > Again, you need to be more specific. We have no idea what your data structure looks like so we have no idea how to help you. This is the way I would do it. class SearchObj(object): def __init__(self): self.database = {} def addElement(astr,alink): self.database[astr] = alink def searchElement(astr): bstr = "blah blah" #insert code to match the element they're trying to search for with the closest one in the database. try: return self.database[bstr] except: pass This sounds like what you're trying to do, but I don't think it's a very good way to make a search engine. no matter how your string-matching is, a single keyword shouldn't be mapped to a single site link. That doesn't make sense! so if someone searches for "Puppy" they'll only get a single link back? What's the point of that? I want to search through the sites not have you search through them for me, find out which you think I want, and only give me the link to that one. If I did I would use Google's I'm Feeling Lucky. For that matter, I would use Google for any search. Why are you wanting to make this search engine? If it's for practice, I feel that it's not a very good project to practice on, because the design issues are so much larger than the programming itself. For practicing programming you should probably use some simple example that requires a lot of code. Like Tetris or Pong. If you're doing this for commercial use, you should just look into adding a Google SiteSearch to your page. python.org did this and it works fantastically. If you insist on continuing in this, I wish you luck and I hope everything turns out how you want it. Are you going to send us a link to it when you're done? -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] for teachers and students: xturtle.py a new tutle graphics module
xturtle.py, extended turtle graphics a new turtle graphics module for Python and Tkinter Version 0.9 of xturtle.py has been released. It can be found at: http://ada.rg16.asn-wien.ac.at/~python/xturtle xturtle should work properly on all major platforms (Mac, Linux and Windows) Feedback would be appreciated to take it into account for polishing it to a final version 1.0. -- Learning to program computer should be fun, for adults and children alike. xturtle.py is a module designed to help you learn computer programming using Python. Turtle graphics provides a means for the beginning programmers to get immediate visual feedback about the correct working of her programs. It's main goal is to provide easy access to a sufficiently rich graphics toolkit. It needs only the standard distribution of python (incl. Tkinter) and imho it (or some descendant of it) once could (and should?) replace turtle.py which is contained in the current standard distribution. xturtle - delivered as a zip-file - contains four main elements: 1. the module xturtle.py . 2. set of 25+ sample scripts of great variety to show possible uses of xturtle.py 3. A simple demoViewer to run and view those sample scripts 4. documentation in form of *.txt files, which also can be viewed with the demoViewer. Docs are derived from doc strings. thus Python's help utility works fine with the module xturtle.py For more information (e.g. some screenshots) see the web page mentioned above. Gregor (It was very convenient for me to use Andre Roberge's last posting as a pattern. You don't mind, Andre?) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] database help for newbie, fetchall()
What is the best way to handle the resutls to a fetchall() command? The result seems to be a list of tuples [(aaa,bbb,ccc0,(11,222,333,)]. I'm new to programming but it seems that what ever I try to accomplish at some point i need the results to end up as strings. Even if you loop through the list you are left with a tuple that represents each column. (aa,bbb,x,ccc) Then you need to loop through the tuple to get your data into strings to use in your app some where. It seems to be more steps then it should be. Is there a cleaner way to do this? ___ No banners. No pop-ups. No kidding. Make My Way your home on the Web - http://www.myway.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MySQLdb: cant get '... where field in %s' to work
I wrote: >> ARTICLES = ('XXX9', 'ABZ2') >> TESTARTICLENAME = """SELECT * FROM tblForTransfer2Prodsite >> WHERE articleName IN %r""" % (ARTICLES,) >> SQLARTICLENAME = """SELECT * FROM tblForTransfer2Prodsite >> WHERE articleName IN %s""" >> >> print cur.execute(TESTARTICLENAME), >> # cannot get this to work >> print cur.execute(SQLARTICLENAME, (ARTICLES,)) Matt wrote: > Can you post your error messages? Sorry, I neglected to state that I do not get any error message. I expected both 'execute' statements to print 2 but the second prints 0. For integers, I get the results I expected. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor