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: - # make a guess
-
guess = tree.getCargo()
-
prompt = "Is it a " + guess + "? "
-
if yes(prompt):
-
print "I rule!"
-
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
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: - # make a guess
-
guess = tree.getCargo()
-
prompt = "Is it a " + guess + "? "
-
if yes(prompt):
-
print "I rule!"
-
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.
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: - # make a guess
-
guess = tree.getCargo()
-
prompt = "Is it a " + guess + "? "
-
if yes(prompt):
-
print "I rule!"
-
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 ....
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: - # make a guess
-
guess = tree.getCargo()
-
prompt = "Is it a " + guess + "? "
-
if yes(prompt):
-
print "I rule!"
-
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.
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. -
def animal():
-
# start with a singleton
-
root = Tree("bird")
-
-
# loop until the user quits
-
while 1:
-
print
-
if not yes("Are you thinking of an animal? "): break
-
-
# walk the tree
-
tree = root
-
while tree.getLeft() != None:
-
prompt = tree.getCargo() + "? "
-
if yes(prompt):
-
tree = tree.getRight()
-
else:
-
tree = tree.getLeft()
-
-
# make a guess
-
guess = tree.getCargo()
-
prompt = "Is it a " + guess + "? "
-
if yes(prompt):
-
print "I rule!"
-
continue
-
-
# get new information
-
prompt = "What is the animal's name? "
-
animal = raw_input(prompt)
-
prompt = "What question would distinguish a %s from a %s? "
-
question = raw_input(prompt % (animal, guess))
-
-
# add new information to the tree
-
tree.setCargo(question)
-
prompt = "If the animal were %s the answer would be? "
-
if yes(prompt % animal):
-
tree.setLeft(Tree(guess))
-
tree.setRight(Tree(animal))
-
else:
-
tree.setLeft(Tree(animal))
-
tree.setRight(Tree(guess))
-
-
def yes(ques):
-
from string import lower
-
ans = lower(raw_input(ques))
-
return (ans[0] == 'y')
-
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. -
def animal():
-
# start with a singleton
-
root = Tree("bird")
-
-
# loop until the user quits
-
while 1:
-
print
-
if not yes("Are you thinking of an animal? "): break
-
-
# walk the tree
-
tree = root
-
while tree.getLeft() != None:
-
prompt = tree.getCargo() + "? "
-
if yes(prompt):
-
tree = tree.getRight()
-
else:
-
tree = tree.getLeft()
-
-
# make a guess
-
guess = tree.getCargo()
-
prompt = "Is it a " + guess + "? "
-
if yes(prompt):
-
print "I rule!"
-
continue
-
-
# get new information
-
prompt = "What is the animal's name? "
-
animal = raw_input(prompt)
-
prompt = "What question would distinguish a %s from a %s? "
-
question = raw_input(prompt % (animal, guess))
-
-
# add new information to the tree
-
tree.setCargo(question)
-
prompt = "If the animal were %s the answer would be? "
-
if yes(prompt % animal):
-
tree.setLeft(Tree(guess))
-
tree.setRight(Tree(animal))
-
else:
-
tree.setLeft(Tree(animal))
-
tree.setRight(Tree(guess))
-
-
def yes(ques):
-
from string import lower
-
ans = lower(raw_input(ques))
-
return (ans[0] == 'y')
-
The continue() statement is used in a loop as I posted earlier. The code you typed in has an indentation error.
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 - #
-
# make a guess
-
guess = tree.getCargo()
-
prompt = "Is it a " + guess + "? "
-
if yes(prompt):
-
print "I rule!"
-
continue
-
-
# get new information
-
prompt = "What is the animal's name? "
-
animal = raw_input(prompt)
-
prompt = "What question would distinguish a %s from a %s? "
-
question = raw_input(prompt % (animal, guess))
-
-
# add new information to the tree
-
tree.setCargo(question)
-
prompt = "If the animal were %s the answer would be? "
-
if yes(prompt % animal):
-
tree.setLeft(Tree(guess))
-
tree.setRight(Tree(animal))
-
else:
-
tree.setLeft(Tree(animal))
-
-
tree.setRight(Tree(guess))
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |