473,574 Members | 18,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

continue problem

pythonner
11 New Member
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 8158
bvdet
2,851 Recognized Expert Moderator Specialist
Hi folks,

I'm working my way through "How to Think Like a Computer Scientist Learning with Python" from this link:
www.greenteapre ss.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 Recognized Expert Contributor
Hi folks,

I'm working my way through "How to Think Like a Computer Scientist Learning with Python" from this link:
www.greenteapre ss.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 Recognized Expert Expert
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
pythonner
11 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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
2531
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
2105
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 statement in it. IE6 seems to have major problems with that as it generates an error "Can't have 'continue' outside of loop". Does anyone know why...
0
369
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 Here's the symptom with a SP containing CONTINUE handlers: If I build the SP via the CLP i.e db2 CREATE PROCEDURE .... , the procedure runs...
46
4182
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 believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
0
1539
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 using W2000KS & IE6 sp1 & VS.NET 2003. If I change method="post" by method="get" form attribute, it works fine.
2
2552
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 page is left blank. I tried to use a code snippet with the "return confirm" function but without success. The code should check whether the textbox...
1
4636
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 validates a user against some Authentication method and then allows him to continue his request if successfully authenticated or denies him access if...
5
3467
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 start debugging and try changing the code, it just ignores any changes I've made. If I add a new line while debugging and try to step through the...
9
1974
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 sync'ed. Well sync them IDE. MS are you there?????? LS
36
2628
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 good principle/s for the good usage of "continue"...or...do any of the regular/irregular contributors have a "favorite" way in which it is...
0
7738
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8081
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7833
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8119
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6482
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2254
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1082
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.