473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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

Similar topics

5
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
6
by: Roebie | last post by:
Hi everyone, I'm having some weird problem with evaluating the continue statement. Within a for loop I'm trying to evaluate a string (generated somewhere earlier) which basically has the continue...
0
by: Paul Reddin | last post by:
Hi, I posted a problem I was having before and got a little side tracked into a solution for that particulcar problem, but there seems to be a more general problem with CONTINUE Handlers in V8.2...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
0
by: Xavier Osa | last post by:
Hi, I have an ASP.Net web page that you can download a file. As Fergunson's problem, it prompts twice dialog boxes only if I select Open button. If I select Save button, it prompts once. I'm...
2
by: buran | last post by:
Dear ASP.NET Programmers, I have a question about a script I'm trying to code and invoke when a button (btnSave) is pressed on the page. This script should only run when a textbox (txtAD) on the...
1
by: ori | last post by:
Hi, I'm facing a problem when trying to continue normal execution flow within a HttpHandler ProcessRequest method. In my application we currently have a custom HttpHandler registered which...
5
by: Diane Yocom | last post by:
I'm using VS2005 and am trying to get Edit and Continue to work while debugging. I've gone to the Tools-Options-Debugging dialog box and made sure "Enable Edit and Continue" is checked, but when I...
9
by: Lloyd Sheen | last post by:
Title says it all. I have reinstalled this "terrible" version of VS now 4 times and I am having trouble with why MS released this. I do a simple one line addition and it tells me thing are not...
36
by: mdh | last post by:
May I ask the group this somewhat non-focused question....having now seen "continue" used in some of the solutions I have worked on. ( Ex 7-4 solution by Tondo and Gimpel comes to mind) Is there a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.