LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   7.3 "How to think like a computer scientist - Python" (https://www.linuxquestions.org/questions/programming-9/7-3-how-to-think-like-a-computer-scientist-python-307084/)

dezza 03-28-2005 01:52 PM

7.3 "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:

Code:

prefixes = "JKLMNOPQ"
suffix = "ack"

for letter in prefixes:
  print letter + suffix

like ..

Code:

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..

APB_4 03-28-2005 03:31 PM

How about
Code:

prefixes = "JKLMNOPQ"
suffix = "ack"

for letter in prefixes:
  if letter=="O":letter=letter+"u"
  elif letter=="Q":letter=letter+"u"
  print letter + suffix


dezza 03-29-2005 09:30 AM

Juack
Kuack
Luack
Muack
Nuack
Ouack
Puack
Quack

Is the output of your code.

APB_4 03-29-2005 09:32 AM

It worked when I did it. Post your code

TzaB 07-09-2008 04:15 PM

First post on your lovely forum, and posting on a bit old post (from 2005). But i want to answer to the problem, because i had the same when this afternoon i tried to do this exercise from the book "How to Think Like a Computer Scientist"

So this is my answer

Code:

prefixes = "JKLMNOPQ"
suffix = "ack"

for x in prefixes:
    if x=="O" or x=="Q":
        print x+"u"+suffix
    else:
        print x+suffix



All times are GMT -5. The time now is 10:09 AM.