Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   "How to think like a computer scientist - Python" (http://www.programmingforums.org/showthread.php?t=2972)

dezza Mar 28th, 2005 12:58 PM

"How to think like a computer scientist - Python"
 
Hello everyone I am glad that I found this forum by searching trough Google, hope you can help me out in my attempt to fully understand Python.

I have followed the guide "How To Think Like a Computerscientist - Python" and i've come to this place where I can't think of any creative ways to do what the exercise says:

As an exercise, modify the program to fix this error.

(It's the Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack Duckling exercise thing I am working on)

http://www.ibiblio.org/obp/thinkCSpy/chap07.htm#3

I know that I could simply just make several of the example code in alphabetical order:

:

prefixes = "JKLMNOPQ"
suffix = "ack"

for letter in prefixes:
  print letter + suffix


like ..

:

prefix1 = "JKLMN"
prefix2 = "O"
prefix3 = "P"
prefix4 = "Q"
suffix1 = "ack"
suffix2 = "uack"

for letter in prefix1:
  print letter + suffix1

for letter in prefix2:
  print letter + suffix2

for letter in prefix3:
  print letter + suffix1

for letter in prefix4:
  print letter + suffix2


But I am not sure if that is what is meant to be done in this guide? Maybe someone who have done this guide can answer this?

Once again, I hope this forum will grow big and become the main spot for programming help..

peace_of_mind Mar 28th, 2005 2:02 PM

You seem to have done exactly what is being asked of you in this exercise. Congrats and keep up the good work.

Moldz Mar 28th, 2005 2:47 PM

I'm not sure if they covered lists at that point, but you could also do:
:

prefix = ['J', 'K', 'L', 'M', 'N', 'Ou', 'P', 'Qu']
suffix = 'ack'
for letter in prefix:
    print letter + suffix

Your solution works, but it's not the easiest code to maintain. Try adding a few prefixes to both codes and see which is easier to modify, ie - make it also say Tack, Uack, Vack. Which is easier?

Berto Mar 29th, 2005 1:51 AM

i would like to say to the poster cheers for the link i have been hunting down a fun python tutorial

dezza Mar 29th, 2005 9:41 AM

Wow, I just got my own thoughts confirmed.. What a really nice place this is, with nice people and positive helping posts, thank you for your introduction :D

#4 (Berto)
Yeah it's really nice, you should check out the other books at http://ibiblio.org/obp especially the Python ones.. Makes programming a little funnier.

I can also recommend these books from a friend who is using them in a programming course he's running at the university he is employed.
http://www.oreilly.com/catalog/python2/index.html
http://www.oreilly.com/catalog/lpython2/index.html

Gotta love Python :D ..
http://home.no/dezza/python
Prolly the youngest python programmer in the world ;) ..
I know this is found on O'Reilly somewhere but I couldn't find the destination and I was to lazy to look for it, just kept it for saving.

#2 (peace_of_mind)
Thank you, and what a nice signature of yours, makes a new programmer welcome into the scene.

#3 (Moldz)
Thanks, worked out really fine! I think it have been mentioned, i've read the book (not fully though) before and I know that there is such a thing as lists :) Like you can do things like this:

:

print prefix[0]+" what a nice functiona!"

By the way, how do I get "return" to print when running the code as script? I know that you can see the + and - in the interpreter, but can you make it print the true/false statement?

I need it in this case:
:

import string
def isLower2(ch):
  return ch in string.lowercase



All times are GMT -5. The time now is 6:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC