wiki:SandBox

Version 14 (modified by apoirier, 9 years ago) (diff)

--

class GuessANumber(component.Task):
   def go(self, comp):
       self.attempt = 1
       number = random.randint(1, 20)

       comp.call(util.Confirm('I choose a number between 1 and 20. Try to guess it'))

       while True:
           x = comp.call(util.Ask('Try #%d: ' % self.attempt))
           if not x.isdigit():
               continue

           x = int(x)

           if x > number:
               comp.call(util.Confirm('Choose a lower number'))

           if x < number:
               comp.call(util.Confirm('Choose a greater number'))

           if x == number:
               comp.call(util.Confirm('Number found in %d attempts' % self.attempt))
               break

           self.attempt += 1

Attachments