469,282 Members | 2,034 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

continue problem

pythonner
Hi folks,

I'm working my way through "How to Think Like a Computer Scientist Learning with Python" from this link:
http://www.greenteapress.com/thinkpy...ml/chap20.html.

In 'The animal tree" program I keep getting a " 'continue' not properly in loop" error. It's coming from the "make a guess" section of the code:

Expand|Select|Wrap|Line Numbers
  1. # make a guess
  2.     guess = tree.getCargo()
  3.     prompt = "Is it a " + guess + "? "
  4.     if yes(prompt):
  5.       print "I rule!"
  6.       continue 

Isn't this incorrect coding in the first place? Isn't continue only used in "for" and "while" loops? That's what I've read in the Python Tutorial. I've tried "break" and "pass" in place of "continue", but none of those work either. How do I get the program to continue on to the next prompt ""What is the animal's name? " ?

Thanks
Aug 28 '07 #1
6 7802
bvdet
2,851 Expert Mod 2GB
Hi folks,

I'm working my way through "How to Think Like a Computer Scientist Learning with Python" from this link:
www.greenteapress.com/thinkpython/thinkCSpy/html/chap20.html.

In 'The animal tree" program I keep getting a " 'continue' not properly in loop" error. It's coming from the "make a guess" section of the code:

Expand|Select|Wrap|Line Numbers
  1. # make a guess
  2.     guess = tree.getCargo()
  3.     prompt = "Is it a " + guess + "? "
  4.     if yes(prompt):
  5.       print "I rule!"
  6.       continue 

Isn't this incorrect coding in the first place? Isn't continue only used in "for" and "while" loops? That's what I've read in the Python Tutorial. I've tried "break" and "pass" in place of "continue", but none of those work either. How do I get the program to continue on to the next prompt ""What is the animal's name? " ?

Thanks
'continue' is used to jump to the next iteration of a loop, skipping the remaining code in the loop body. I do not see a loop in your code.
Aug 28 '07 #2
ghostdog74
511 Expert 256MB
Hi folks,

I'm working my way through "How to Think Like a Computer Scientist Learning with Python" from this link:
www.greenteapress.com/thinkpython/thinkCSpy/html/chap20.html.

In 'The animal tree" program I keep getting a " 'continue' not properly in loop" error. It's coming from the "make a guess" section of the code:

Expand|Select|Wrap|Line Numbers
  1. # make a guess
  2.     guess = tree.getCargo()
  3.     prompt = "Is it a " + guess + "? "
  4.     if yes(prompt):
  5.       print "I rule!"
  6.       continue 

Isn't this incorrect coding in the first place? Isn't continue only used in "for" and "while" loops? That's what I've read in the Python Tutorial. I've tried "break" and "pass" in place of "continue", but none of those work either. How do I get the program to continue on to the next prompt ""What is the animal's name? " ?

Thanks
read the whole code carefully in that link you gave. there are a number of while loops ....
Aug 28 '07 #3
bartonc
6,596 Expert 4TB
Hi folks,

I'm working my way through "How to Think Like a Computer Scientist Learning with Python" from this link:
http://www.greenteapress.com/thinkpy...ml/chap20.html.

In 'The animal tree" program I keep getting a " 'continue' not properly in loop" error. It's coming from the "make a guess" section of the code:

Expand|Select|Wrap|Line Numbers
  1. # make a guess
  2.     guess = tree.getCargo()
  3.     prompt = "Is it a " + guess + "? "
  4.     if yes(prompt):
  5.       print "I rule!"
  6.       continue 

Isn't this incorrect coding in the first place? Isn't continue only used in "for" and "while" loops? That's what I've read in the Python Tutorial. I've tried "break" and "pass" in place of "continue", but none of those work either. How do I get the program to continue on to the next prompt ""What is the animal's name? " ?

Thanks
The link is approved! In fact, i think that somebody aught to add Green Tea Press to one of the Python Resource threads. (Unless, of course, all their code is really broken:<)



Please post more of the code that you have. There is a possibility that you have a copy&paste error, or something.
Aug 29 '07 #4
Okay, here's the complete code. I didn't cut-and-paste, I typed this out, straight from the web site. My point is, I don't think the "continue" statement belongs in an "if" block. Am I right, or not? Because everytime I run the program I get a 'continue' not properly in loop" error.

Expand|Select|Wrap|Line Numbers
  1. def animal():
  2.     # start with a singleton
  3.     root = Tree("bird")
  4.  
  5.     # loop until the user quits
  6.     while 1:
  7.         print
  8.         if not yes("Are you thinking of an animal? "): break
  9.  
  10.     # walk the tree
  11.     tree = root
  12.     while tree.getLeft() != None:
  13.         prompt = tree.getCargo() + "? "
  14.         if yes(prompt):
  15.             tree = tree.getRight()
  16.         else:
  17.             tree = tree.getLeft()
  18.  
  19.     # make a guess
  20.     guess = tree.getCargo()
  21.     prompt = "Is it a " + guess + "? "
  22.     if yes(prompt):
  23.         print "I rule!"
  24.         continue
  25.  
  26.     # get new information
  27.     prompt = "What is the animal's name? "
  28.     animal = raw_input(prompt)
  29.     prompt = "What question would distinguish a %s from a %s? "
  30.     question = raw_input(prompt % (animal, guess))
  31.  
  32.     # add new information to the tree
  33.     tree.setCargo(question)
  34.     prompt = "If the animal were %s the answer would be? "
  35.     if yes(prompt % animal):
  36.         tree.setLeft(Tree(guess))
  37.         tree.setRight(Tree(animal))
  38.     else:
  39.         tree.setLeft(Tree(animal))
  40.         tree.setRight(Tree(guess))
  41.  
  42. def yes(ques):
  43.     from string import lower
  44.     ans = lower(raw_input(ques))
  45.     return (ans[0] == 'y')
  46.  
Aug 30 '07 #5
bvdet
2,851 Expert Mod 2GB
Okay, here's the complete code. I didn't cut-and-paste, I typed this out, straight from the web site. My point is, I don't think the "continue" statement belongs in an "if" block. Am I right, or not? Because everytime I run the program I get a 'continue' not properly in loop" error.

Expand|Select|Wrap|Line Numbers
  1. def animal():
  2.     # start with a singleton
  3.     root = Tree("bird")
  4.  
  5.     # loop until the user quits
  6.     while 1:
  7.         print
  8.         if not yes("Are you thinking of an animal? "): break
  9.  
  10.     # walk the tree
  11.     tree = root
  12.     while tree.getLeft() != None:
  13.         prompt = tree.getCargo() + "? "
  14.         if yes(prompt):
  15.             tree = tree.getRight()
  16.         else:
  17.             tree = tree.getLeft()
  18.  
  19.     # make a guess
  20.     guess = tree.getCargo()
  21.     prompt = "Is it a " + guess + "? "
  22.     if yes(prompt):
  23.         print "I rule!"
  24.         continue
  25.  
  26.     # get new information
  27.     prompt = "What is the animal's name? "
  28.     animal = raw_input(prompt)
  29.     prompt = "What question would distinguish a %s from a %s? "
  30.     question = raw_input(prompt % (animal, guess))
  31.  
  32.     # add new information to the tree
  33.     tree.setCargo(question)
  34.     prompt = "If the animal were %s the answer would be? "
  35.     if yes(prompt % animal):
  36.         tree.setLeft(Tree(guess))
  37.         tree.setRight(Tree(animal))
  38.     else:
  39.         tree.setLeft(Tree(animal))
  40.         tree.setRight(Tree(guess))
  41.  
  42. def yes(ques):
  43.     from string import lower
  44.     ans = lower(raw_input(ques))
  45.     return (ans[0] == 'y')
  46.  
The continue() statement is used in a loop as I posted earlier. The code you typed in has an indentation error.
Aug 30 '07 #6
varuns
39
The continue() statement is used in a loop as I posted earlier. The code you typed in has an indentation error.

Bvdet is Correct.
Try prefixing tab on following lines

Expand|Select|Wrap|Line Numbers
  1. #
  2. # make a guess
  3.     guess = tree.getCargo()
  4.     prompt = "Is it a " + guess + "? "
  5.     if yes(prompt):
  6.         print "I rule!"
  7.         continue
  8.  
  9.     # get new information
  10.     prompt = "What is the animal's name? "
  11.     animal = raw_input(prompt)
  12.     prompt = "What question would distinguish a %s from a %s? "
  13.     question = raw_input(prompt % (animal, guess))
  14.  
  15.     # add new information to the tree
  16.     tree.setCargo(question)
  17.     prompt = "If the animal were %s the answer would be? "
  18.     if yes(prompt % animal):
  19.         tree.setLeft(Tree(guess))
  20.         tree.setRight(Tree(animal))
  21.     else:
  22.         tree.setLeft(Tree(animal))
  23.  
  24.         tree.setRight(Tree(guess))
Sep 3 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Ann | last post: by
6 posts views Thread by Roebie | last post: by
reply views Thread by Paul Reddin | last post: by
46 posts views Thread by Keith K | last post: by
5 posts views Thread by Diane Yocom | last post: by
9 posts views Thread by Lloyd Sheen | last post: by
36 posts views Thread by mdh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.