continue problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pythonner
    New Member
    • Aug 2007
    • 11

    continue problem

    Hi folks,

    I'm working my way through "How to Think Like a Computer Scientist Learning with Python" from this link:
    https://www.greenteapress.com/thinkp...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:

    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by pythonner
    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:

    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.

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by pythonner
      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:

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

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by pythonner
        Hi folks,

        I'm working my way through "How to Think Like a Computer Scientist Learning with Python" from this link:
        https://www.greenteapress.com/thinkp...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:

        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.

        Comment

        • pythonner
          New Member
          • Aug 2007
          • 11

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

          Code:
          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')

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by pythonner
            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.

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

            Comment

            • varuns
              New Member
              • Jun 2007
              • 39

              #7
              Originally posted by bvdet
              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

              Code:
              #
              # 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))

              Comment

              Working...