Re: [Tutor] Medical Decision-Making Question
Fred G wrote: Thanks guys for all the feedback. re Jim's comments: I completely agree that the difference b/t "slight" fever and "returning" fever, etc will pose some problems. My hunch is that initially I'll just do something like make "fever" be the only one for now Any qualitative rating system is going to be subjective. If this expert system is aimed at doctors, you may be able to ask for actual temperatures rather than just "fever". Otherwise, you will have to come up with your own idea for what "slight" vs. "strong" fever might mean. The accuracy of the diagnosis will depend in part on how well the user's idea of "slight" matches yours. re Steve's comments: hmm, sounds like I really should take an AI class. This problem is just really exciting and driving me, and I'm glad you pointed out that this will probably take a lot more time than I had predicted. I'm pretty motivated personally to solve it. I had a few more questions about your code: a) Why did you choose not to use a dictionary to store the diseases (keys) and their possible values (symptoms)? That seemed the most intuitive to me, Because it was nearly 2am when I came up with the idea and it was just the first thing that popped into my head. Don't imagine that I had sat down and designed the application! :) You're right though that good design of your data structures is vital. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Break stament issue
Susana Iraiis Delgado Rodriguez wrote: Hello members!! Steven, I already changed the settings in the IDE to avoid the trouble when I type the code. In the other hand I added the pass statement so the script keep working even though it finds an error, but the scripts ignore the pass statement. Console prints: Traceback (most recent call last): File "", line 1, in File "mapnik_punto_sin_duda.py", line 44, in lyr.datasource = mapnik.Shapefile(base=ruta,file=archivo[0]) File "C:\mapnik-0.7.1\python\2.6\site-packages\mapnik\__init__.py", line 282, in Shapefile return CreateDatasource(keywords) RuntimeError: wrong file code : -1997790976 This looks like an internal error of mapnik. I know nothing about mapnik, but my wild guess is that it expects a filename and you are giving it something else? Or the wrong filename? Or maybe it expects an open file, and you have given it a filename? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Step Value
Vincent Balmori wrote: Here is my updated code. As simple as this may be, I am a little lost again. I appreciate the help and explanations to try to push me to get this on my own, but at this point (especially after one week) this is when me being given the answer with an explanation will help me much more, so I can understand how it works better. def ask_number(question, low, high, step = 1): """Ask for a number within a range.""" response = None while response not in range(low, high, step): response = int(input(question)) return response You've got it now! Well done. The trick is, you have to include the argument in the function parameter list, AND give it a value. The part "step=1" inside the parentheses of the "def" line does exactly that. def ask_number(question, low, high, step=1): ^^ Suppose you call the function like this: ask_number("hello", 1, 10) Python takes the arguments you give from left to right and assigns them to the function parameters: question = "hello" low = 1 high = 10 step = ??? no value given Because you haven't supplied a value for step, Python next looks for a default, and finds the value 1, so it uses that instead of raising an exception (and error message). -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Main Function
I answered another question that says "Modify the guess_number program so that the program's code is in a function called main(). Don't forget to call main() so you can play the game." It seems to be working fine, but I would like to have a second opinion if there was a better way to put it together. # Guess My Number # # The computer picks a random number between 1 and 10 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money import random def display_instruct(): print("\tWelcome to 'Guess My Number'!") print("\nI'm thinking of a number between 1 and 10.") print("Try to guess it in as few attempts as possible.\n") # set ask_number function def ask_number(question, low, high, step = 1): """Ask for a number within a range.""" response = None while response not in range(low, high, step): response = int(input(question)) return response # guessing loop def num(): # set the initial values the_number = random.randint(1, 10) tries = 0 guess = None while guess != the_number and tries < 4: guess = ask_number("Take a guess:", 1, 10) if guess > the_number: print("Lower...") else: print("Higher...") tries += 1 if tries == 4: print("\nYou fail!") input("\n\nPress the enter key to exit.") break while guess == the_number: print("\nYou guessed it! The number was", the_number) print("And it only took you", tries, "tries!\n") input("\n\nPress the enter key to exit.") break def main(): display_instruct() num() # start the program main() input("\n\nPress the enter key to quit.") -- View this message in context: http://old.nabble.com/Main-Function-tp31873480p31873480.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need script help with concept
On Sat, Jun 18, 2011 at 2:13 AM, Corey Richardson wrote: > "Noah Hall" wrote > >> Of course, if you mean *completely in-place replace* the question, >> then you >> will need something like ncurses (if on Linux) - >> http://docs.python.org/library/curses.html > > There also exists urwid, which is multiplatform, and is also less painful to > use. I hear > there is a port for Python 3 somewhere too. http://excess.org/urwid/ > (Apologies if email is HTML, using a web client) I'm pretty sure that's not native to Windows; you'd have to use Cygwin, in which case you might as well use curses. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Seattle PyCamp 2011
On Sat, Jun 18, 2011 at 2:15 AM, Steven D'Aprano wrote: > Noah Hall wrote: > >> Just a note, but are these questions jokes? >> >>> Know how to use a text editor (not a word processor, but a text editor)? >>> Know how to use a browser to download a file? >>> Know how to run a program installer? >> >> If not, then I'd consider removing them. This isn't 1984. > > I think the questions are fine. It indicates the level of technical > knowledge required -- not much, but more than just the ability to sign in to > AOL. > > In 1984 the newbies didn't know anything about computers *and knew they > didn't know*, but now you have people who think that because they can write > a letter in Microsoft Office and save as HTML, they're expert at > programming. > > I wish I were joking but I've had to work for some of them. That's true, I suppose, but in that case the rest of the questions are out of place. I believe that someone who knows what environmental variables are and how to change them is a huge step up from someone who knows how to *download things*. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need script help with concept
"Noah Hall" wrote Of course, if you mean *completely in-place replace* the question, then you will need something like ncurses (if on Linux) - http://docs.python.org/library/curses.html There also exists urwid, which is multiplatform, and is also less painful to use. I hear there is a port for Python 3 somewhere too. http://excess.org/urwid/ (Apologies if email is HTML, using a web client) -- Corey Richardson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Seattle PyCamp 2011
Noah Hall wrote: Just a note, but are these questions jokes? Know how to use a text editor (not a word processor, but a text editor)? Know how to use a browser to download a file? Know how to run a program installer? If not, then I'd consider removing them. This isn't 1984. I think the questions are fine. It indicates the level of technical knowledge required -- not much, but more than just the ability to sign in to AOL. In 1984 the newbies didn't know anything about computers *and knew they didn't know*, but now you have people who think that because they can write a letter in Microsoft Office and save as HTML, they're expert at programming. I wish I were joking but I've had to work for some of them. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need script help with concept
"Noah Hall" wrote Of course, if you mean *completely in-place replace* the question, then you will need something like ncurses (if on Linux) - http://docs.python.org/library/curses.html Well, for this you could just use a lot of print statements(in a loop) or print a lot of newlines... curses would be nicer but not really necessary. But I aghree, it does sound close to homework... Alan G., ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Weird tkFont behavior
"Steve Willoughby" wrote I've been getting happier to see the improvements to what you can do with just plain Tkinter since the last time I used it seriously. I agree, Tkinter still gets a lot of bad feedback about its look but with ttk that's no longer justified. wxPython still has the edge for fully featured GUI work but for basic GUI work Tkinter/ttk is easier and works "out of the box" with most python installations. Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Step Value
>> def ask_number(question, low, high, step = 1): >> """Ask for a number within a range.""" >> response = None >> while response not in range(low, high, step): >> response = int(input(question)) >> return response > > With the only comment being that you don't really need the > response=None line because response always gets set > inside the loop. > But the value of response is used in starting the loop (it is needed > to test whether the loop should be gone through a first time), so one > _does_ need that line. Doh! Stoopid me, of course it is. So the function is pretty much fine as it is. Apologies, Alan G.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Seattle PyCamp 2011
On Sat, Jun 18, 2011 at 12:46 AM, Chris Calloway wrote: > University of Washington Marketing and the Seattle Plone Gathering host the > inaugural Seattle PyCamp 2011 at The Paul G. Allen Center for Computer > Science & Engineering on Monday, August 29 through Friday, September 2, > 2011. > > Register today at http://trizpug.org/boot-camp/seapy11/ > > For beginners, this ultra-low-cost Python Boot Camp makes you productive so > you can get your work done quickly. PyCamp emphasizes the features which > make Python a simpler and more efficient language. Following along with > example Python PushUps™ speeds your learning process. Become a > self-sufficient Python developer in just five days at PyCamp! PyCamp is > conducted on the campus of the University of Washington in a state of the > art high technology classroom. Just a note, but are these questions jokes? >Know how to use a text editor (not a word processor, but a text editor)? >Know how to use a browser to download a file? >Know how to run a program installer? If not, then I'd consider removing them. This isn't 1984. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Seattle PyCamp 2011
University of Washington Marketing and the Seattle Plone Gathering host the inaugural Seattle PyCamp 2011 at The Paul G. Allen Center for Computer Science & Engineering on Monday, August 29 through Friday, September 2, 2011. Register today at http://trizpug.org/boot-camp/seapy11/ For beginners, this ultra-low-cost Python Boot Camp makes you productive so you can get your work done quickly. PyCamp emphasizes the features which make Python a simpler and more efficient language. Following along with example Python PushUps™ speeds your learning process. Become a self-sufficient Python developer in just five days at PyCamp! PyCamp is conducted on the campus of the University of Washington in a state of the art high technology classroom. -- Sincerely, Chris Calloway http://nccoos.org/Members/cbc office: 3313 Venable Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Step Value
On Fri, Jun 17, 2011 at 11:03 PM, Alan Gauld wrote: > > "Vincent Balmori" wrote > >> Here is my updated code. As simple as this may be, I am a little lost >> again. > > I'm not sure why you are lost because that's pretty much it. > >> ... at this point (especially after one week) this is when me being >> given the answer with an explanation will help me much more, so I can >> understand how it works better. > > The answer is: > >> def ask_number(question, low, high, step = 1): >> """Ask for a number within a range.""" >> response = None >> while response not in range(low, high, step): >> response = int(input(question)) >> return response > > With the only comment being that you don't really need the > response=None line because response always gets set > inside the loop. But the value of response is used in starting the loop (it is needed to test whether the loop should be gone through a first time), so one _does_ need that line. -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need script help with concept
On Fri, Jun 17, 2011 at 11:34 PM, Victor wrote: > I am in the process of building a script but I do not know if what I am > trying to do is possible. So, long story short, I need help. > > The concept: > I am want to be able to ask the user a series of questions in the program > window. > > But here is the action I want to appear on the screen. > > 0. Question pops up > 1. User inputs answer > 2. User press enter > 3. The first question and answer is replaced with a new question on the > screen > 4. User answers the second question > 5. User press enter > 6. Question and answer disappears > 7. etc. > > All user inputs are placed in a variable. > > Please help. :-) > One way I read this (not sure if this is what you meant, if so, skip down to the bottom): Very possible, and very easy. Sounds a bit too much like a homework question for me to give you complete help ;), however - Look up raw_input. >>> a_variable = raw_input('Is this a question?') Is this a question?Yes >>> print a_variable Yes Make a program, or attempt to, and post back here with any questions. Of course, if you mean *completely in-place replace* the question, then you will need something like ncurses (if on Linux) - http://docs.python.org/library/curses.html If on Windows, have a look at - http://effbot.org/zone/console-index.htm HTH. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Need script help with concept
I am in the process of building a script but I do not know if what I am trying to do is possible. So, long story short, I need help. The concept: I am want to be able to ask the user a series of questions in the program window. But here is the action I want to appear on the screen. 0. Question pops up 1. User inputs answer 2. User press enter 3. The first question and answer is replaced with a new question on the screen 4. User answers the second question 5. User press enter 6. Question and answer disappears 7. etc. All user inputs are placed in a variable. Please help. :-) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Weird tkFont behavior
On 17-Jun-11 14:18, Alan Gauld wrote: But I confess I've never used font tags (and didn't even know they existed). Neither did I until now. I had been playing with WxPython previously (which is still a great toolkit I'd recommend when it makes sense), but I've been getting happier to see the improvements to what you can do with just plain Tkinter since the last time I used it seriously. Tkinter and ttk do have the advantage of already being right there in the standard library. -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Weird tkFont behavior
"Steve Willoughby" wrote I think I solved it, actually.. as I was typing this up, I wondered in passing about this: of the other fonts I configured for the other tags. Do I need to keep other references to the tkFont objects somewhere else or something? I was going to suggest trying that and if it didn't work to ask on the tkinter group, they are usually pretty responsive. But I confess I've never used font tags (and didn't even know they existed). Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Communicating Between Programs Using A Raw Input (Cont'd)
"Jacob Bender" wrote did have one question however, and that is will I need to modify the code of my password program? No, if it uses raw_input and print it will be using atdin and stdout so you can pipe from one program into the other Here's the code again: password = "Helloworld" try= raw_input("What's the password?") while try != password: try = raw_input("Incorrect, what's the password?") Just try it and see. If it doesn't work get back to us - with the output of course :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Step Value
"Vincent Balmori" wrote Here is my updated code. As simple as this may be, I am a little lost again. I'm not sure why you are lost because that's pretty much it. ... at this point (especially after one week) this is when me being given the answer with an explanation will help me much more, so I can understand how it works better. The answer is: def ask_number(question, low, high, step = 1): """Ask for a number within a range.""" response = None while response not in range(low, high, step): response = int(input(question)) return response With the only comment being that you don't really need the response=None line because response always gets set inside the loop. If you don't understand how your code answered the question, feel free to reply with specific issues, like a particular statement you don't get or whatever. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Step Value
Here is my updated code. As simple as this may be, I am a little lost again. I appreciate the help and explanations to try to push me to get this on my own, but at this point (especially after one week) this is when me being given the answer with an explanation will help me much more, so I can understand how it works better. def ask_number(question, low, high, step = 1): """Ask for a number within a range.""" response = None while response not in range(low, high, step): response = int(input(question)) return response -Vincent Alan Gauld wrote: > > > "Vincent Balmori" wrote > > "def spam(n=3): > """Return n slices of yummy spam.""" > return "spam "*n > > >> This is my new code for a default step value based on your feedback >> Steve: >> >> def ask_number(question, low, high, step): >> """Ask for a number within a range.""" >> response = None >> if step == None: >> step = 1 > > Nope, you are still missing the point. > Look again at how Steven defined his function. > What is different about his definition of n and your definition of > step? > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- View this message in context: http://old.nabble.com/-Tutor--Step-Value-tp31865967p31871108.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Communicating Between Programs Using A Raw Input (Cont'd)
Dear Tutors, Alright, I read up on stdin and stdout and I did find how useful they are. I did have one question however, and that is will I need to modify the code of my password program? Here's the code again: password = "Helloworld" try= raw_input("What's the password?") while try != password: try = raw_input("Incorrect, what's the password?") And if so, then should I use sys.stdin.read() and sys.stdout.write() for the program that generates the random passwords or are there other commands that I should use? Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading opened files
On Friday 17 June 2011 17:42:29 Walter Prins wrote: > On 17 June 2011 17:20, Lisi wrote: > > >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r") > > >>> file.close() > > >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r") > > >>> whole=file.read > > >>> print whole > > > > > > > > >>> print "%r" % whole > > > > > > > > >>> print "whole is %r" %whole > > > > whole is > > > > >>> print "whole is %r" % whole > > > > whole is > > You're missing the () off the whole=file.read() call. > > Ask youself, what is "file.read"? It is of course a method of the "file" > object. And, in fact that's exactly what Python itself is telling you > also. So when you say: > > whole=file.read > > You're assigning the method itself, to the name "whole". Consequently, you > would be able to do: > > something = whole() > > ... which would then *call* the function using the name "whole", which > would be identical to calling that same function via "file.read". > > To reiterate, there's a difference between just referencing a method or > function and actually calling it. To call it you need to use parentheses. Thanks, Walter. That is also very useful and clear. As with James's answer, I have left this intact for the archives. It should be available for other newbies who are blundering about a bit. Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading opened files
Your problem is right here: >>> whole=file.read >>> print whole Your re-assigning the method "read()", which is a method of the object "file" to the variable "whole" So, when you print "whole" you can see that it is printing the location of the method in memory. If you were to print file.read you would get the same results. Now, to call the method (or a function) you need to add the parentheses with any arguments that it may need. In your case, you need to do "read()". This principle holds true with any object. So, let's say we created a list object by doing this mylist = [] mylist now has all of the methods that a list has. So, for example, I can append something to the list by going: mylist.append(1) printing my list will show a list like this [1] IDLE 2.6.5 >>> mylist = [] >>> mylist.append(1) >>> print mylist [1] but instead lets say I did this: >>> b = mylist.append >>> print b Which can be handy if i needed to do appending all the time, for example: >>> b(1) >>> print mylist [1, 1] My last piece of advice here is, use the Python documentation in addition to googling. It's actually very readable (I think anyway) On Fri, Jun 17, 2011 at 12:20 PM, Lisi wrote: > Hello :-) > > I have got as far as I have,i.e. apparently succeeding in both opening and > closing two different files, thanks to google, but my struggles to _do_ > something with a file that I have opened are getting me nowhere. Here is > my > latest failure: > > >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r") > >>> file.close() > >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r") > >>> whole=file.read > >>> print whole > > >>> print "%r" % whole > > >>> print "whole is %r" %whole > whole is > >>> print "whole is %r" % whole > whole is > >>> > > I'd be extremely grateful if one of you was willing to drop a hint, give me > some pointers (even e.g. guide me to asking the correct question of > Google), > or tell me where I am going wrong. > > In general, the author advises leaving any of the extra credit questions > that > you are struggling with and coming back to them later. And in general I > have > found that that works. But this set of extra-credit questions he advises > mastering before moving on. And I am stuck on this one at this stage. :-( > (I think that I have cracked the others.) > > Thanks. > > Lisi > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading opened files
On 17 June 2011 17:20, Lisi wrote: > >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r") > >>> file.close() > >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r") > >>> whole=file.read > >>> print whole > > >>> print "%r" % whole > > >>> print "whole is %r" %whole > whole is > >>> print "whole is %r" % whole > whole is > >>> > You're missing the () off the whole=file.read() call. Ask youself, what is "file.read"? It is of course a method of the "file" object. And, in fact that's exactly what Python itself is telling you also. So when you say: whole=file.read You're assigning the method itself, to the name "whole". Consequently, you would be able to do: something = whole() ... which would then *call* the function using the name "whole", which would be identical to calling that same function via "file.read". To reiterate, there's a difference between just referencing a method or function and actually calling it. To call it you need to use parentheses. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Reading opened files (SOLVED)
So sorry to have troubled you all. The light suddenly dawned. Perhaps because I was more relaxed, having asked the list? Anyhow, I now know how to do it, and it is of course, simple. [Passing on and coming back would obviously have worked, but the author had said not to do that this time. :-( ] Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Reading opened files
Hello :-) I have got as far as I have,i.e. apparently succeeding in both opening and closing two different files, thanks to google, but my struggles to _do_ something with a file that I have opened are getting me nowhere. Here is my latest failure: >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r") >>> file.close() >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r") >>> whole=file.read >>> print whole >>> print "%r" % whole >>> print "whole is %r" %whole whole is >>> print "whole is %r" % whole whole is >>> I'd be extremely grateful if one of you was willing to drop a hint, give me some pointers (even e.g. guide me to asking the correct question of Google), or tell me where I am going wrong. In general, the author advises leaving any of the extra credit questions that you are struggling with and coming back to them later. And in general I have found that that works. But this set of extra-credit questions he advises mastering before moving on. And I am stuck on this one at this stage. :-( (I think that I have cracked the others.) Thanks. Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] File parsing
"Neha P" wrote for eachline in f_obj: eachline=eachline[ :-1]# to eliminate the trailing "\n" Better to use rstrip() here, thee might be extraneous spaces etc to remove too. list_words=eachline.split(" ") list_words[0]=list_words[0]+"\n"# to add "\n" so that after line 1 is printed, line 2 should start on a new line I'd do this at the end after you print the line, otherwise if you later decide to rearrange the words you have to remember to remove the \n from that specific word. You don't really want that word to have a \n you want the printed line to have a \n, so put it on the line not the word. list_words.reverse() for every_word in list_words: print every_word,# 'comma' helps in printing words on same line,hence for last word we append "\n" You could use join() and just print that: print ' '.join(list_words) And print will now add the newline for you. HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Step Value
"Vincent Balmori" wrote "def spam(n=3): """Return n slices of yummy spam.""" return "spam "*n This is my new code for a default step value based on your feedback Steve: def ask_number(question, low, high, step): """Ask for a number within a range.""" response = None if step == None: step = 1 Nope, you are still missing the point. Look again at how Steven defined his function. What is different about his definition of n and your definition of step? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem
self doesn't refer to anything? It is undefined because it is in main and not in a method of an object. You have to do crit.talk() where crit is an object (which by the way isn't, you have crit1 and crit2) On 17 June 2011 09:28, David Merrick wrote: > # Critter Caretaker > # A virtual pet to care for > class Farm(object): > #A collection of Critters > > def talk(self,farm): > farm.talk() > > > > class Critter(object): > > """A virtual pet""" > def __init__(self, name, hunger = 0, boredom = 0): > self.name = name > self.hunger = hunger > self.boredom = boredom > > # __ denotes private method > def __pass_time(self,farm): > self.hunger += 1 > self.boredom += 1 > self.__str__() > > def __str__(self,farm): > print("Hunger is",self.hunger, "Boredom is " ,self.boredom) > print("Unhappines is ",self.hunger + self.boredom," and Mood is > ",self.mood) > > > > @property > def mood(self,farm): > unhappiness = self.hunger + self.boredom > if unhappiness < 5: > m = "happy" > elif 5 <= unhappiness <= 10: > m = "okay" > elif 11 <= unhappiness <= 15: > m = "frustrated" > else: > m = "mad" > return m > > def talk(self,farm): > print("I'm", self.name, "and I feel", self.mood, "now.\n") > self.__pass_time() > > > def eat(self,farm): > food = int(input("Enter how much food you want to feed your critter: > ")) > print("Brruppp. Thank you.") > self.hunger -= food > # hunger = 0 at iniatition > # self.hunger = self.boredom - food > if self.hunger < 0: > self.hunger = 0 > self.__pass_time() > > > def play(self,farm): > fun = int(input("Enter how much fun you want your critter to have: > ")) > print("Wheee!") > self.boredom -= fun > # boredom = 0 at iniatition > # self.boredom = self.boredom - fun > if self.boredom < 0: > self.boredom = 0 > self.__pass_time() > > > def main(): > ## crit_name = input("What do you want to name your critter?: ") > ## crit = Critter(crit_name) > > crit1 = Critter("Sweetie") > crit2 = Critter("Dave") > farm = [crit1,crit2] > > choice = None > while choice != "0": > print \ > (""" > Critter Caretaker > > 0 - Quit > 1 - Listen to your critter > 2 - Feed your critter > 3 - Play with your critter > """) > > choice = input("Choice: ") > print() > > # exit > if choice == "0": > print("Good-bye.") > > # listen to your critter > elif choice == "1": > self.talk() > > # feed your critter > elif choice == "2": > crit.eat() > > # play with your critter > elif choice == "3": > crit.play() > > # some unknown choice > else: > print("\nSorry, but", choice, "isn't a valid choice.") > > > > > > main() > ("\n\nPress the enter key to exit.") > > Critter Caretaker > > 0 - Quit > 1 - Listen to your critter > 2 - Feed your critter > 3 - Play with your critter > > Choice: 1 > > Traceback (most recent call last): > File "D:\David\Python\programs\critter_farm2.py", line 118, in > main() > File "D:\David\Python\programs\critter_farm2.py", line 100, in main > self.talk() > NameError: global name 'self' is not defined > > -- > Dave Merrick > > merrick...@gmail.com > > Ph 03 3423 121 > Cell 027 3089 169 > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem
# Critter Caretaker # A virtual pet to care for class Farm(object): #A collection of Critters def talk(self,farm): farm.talk() class Critter(object): """A virtual pet""" def __init__(self, name, hunger = 0, boredom = 0): self.name = name self.hunger = hunger self.boredom = boredom # __ denotes private method def __pass_time(self,farm): self.hunger += 1 self.boredom += 1 self.__str__() def __str__(self,farm): print("Hunger is",self.hunger, "Boredom is " ,self.boredom) print("Unhappines is ",self.hunger + self.boredom," and Mood is ",self.mood) @property def mood(self,farm): unhappiness = self.hunger + self.boredom if unhappiness < 5: m = "happy" elif 5 <= unhappiness <= 10: m = "okay" elif 11 <= unhappiness <= 15: m = "frustrated" else: m = "mad" return m def talk(self,farm): print("I'm", self.name, "and I feel", self.mood, "now.\n") self.__pass_time() def eat(self,farm): food = int(input("Enter how much food you want to feed your critter: ")) print("Brruppp. Thank you.") self.hunger -= food # hunger = 0 at iniatition # self.hunger = self.boredom - food if self.hunger < 0: self.hunger = 0 self.__pass_time() def play(self,farm): fun = int(input("Enter how much fun you want your critter to have: ")) print("Wheee!") self.boredom -= fun # boredom = 0 at iniatition # self.boredom = self.boredom - fun if self.boredom < 0: self.boredom = 0 self.__pass_time() def main(): ##crit_name = input("What do you want to name your critter?: ") ##crit = Critter(crit_name) crit1 = Critter("Sweetie") crit2 = Critter("Dave") farm = [crit1,crit2] choice = None while choice != "0": print \ (""" Critter Caretaker 0 - Quit 1 - Listen to your critter 2 - Feed your critter 3 - Play with your critter """) choice = input("Choice: ") print() # exit if choice == "0": print("Good-bye.") # listen to your critter elif choice == "1": self.talk() # feed your critter elif choice == "2": crit.eat() # play with your critter elif choice == "3": crit.play() # some unknown choice else: print("\nSorry, but", choice, "isn't a valid choice.") main() ("\n\nPress the enter key to exit.") Critter Caretaker 0 - Quit 1 - Listen to your critter 2 - Feed your critter 3 - Play with your critter Choice: 1 Traceback (most recent call last): File "D:\David\Python\programs\critter_farm2.py", line 118, in main() File "D:\David\Python\programs\critter_farm2.py", line 100, in main self.talk() NameError: global name 'self' is not defined >>> -- Dave Merrick merrick...@gmail.com Ph 03 3423 121 Cell 027 3089 169 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor