Calling a function with a step value.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cjllewey369
    New Member
    • Mar 2007
    • 14

    Calling a function with a step value.

    "Improve the function ask_number() so that the function can be called with a step value. Make the default value of step 1."

    Those are the directions I have to follow. I dont know what a step function is, so I dont know how to change ask_number() to be called by one.

    here is ask_number:

    Code:
    def ask_number(question, low, high):
        """Ask a yes or no question."""
        response = None
        while response not in range(low, high):
            response = int(raw_input(question))
        return response
    Thank you for your help!
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    Found a reference to the question you are asking at

    though I can't really see what or why you would even want to step something in the example.

    Comment

    • Cjllewey369
      New Member
      • Mar 2007
      • 14

      #3
      well, i am to improve ask_number to call the function with a step value, and then i am supposed to use the function ask_number() in this:

      Code:
      # Guess My Number
      #
      # The computer picks a random number between 1 and 100
      # The player tries to guess it and the computer lets
      # the player know if the guess is too high, too low
      # or right on the money
      
      import random
      
      print "\tWelcome to 'Guess My Number'!"
      print "\nI'm thinking of a number between 1 and 100."
      print "Try to guess it in as few attempts as possible.\n"
      
      # set the initial values
      the_number = random.randrange(100) + 1
      guess = int(raw_input("Take a guess: "))
      tries = 1
      
      #guessing loop
      while (guess != the_number):
          if (guess > the_number):
              print "Lower..."
              tries += 1
          else:
              print "Higher..."
              tries += 1
      
          guess = int(raw_input("Take a guess: "))
      
      print "You guessed it! The number was", the_number
      print "And it only took you", tries, "tries!\n"
      
      raw_input("\n\nPress the enter key to exit.")
      Then I am supposed to modify that to put the main code into a function called main()

      But back to my original question. I don't see what changing it will do to benefit me. I understand that i could use it in it's current form, but adding a step value? What? I am not exactly sure how clear I am being with what I am required to do, so I will lay it out once more.

      1. Improve the function ask_number() so that the function can be called with a step value. Make the default value of step 1. --ask_number() is provided by the book I am using and is in my first post.

      2. Modify the Guess My Number chapter project from Chapter 3 by reusing the function ask_number() --The code for Guess My Number is up above and is also provided by the book I am using.

      3. Modify the new version of Guess My Number you created in the the last challenge(numbe r 2) so that the program's code is in a function called main(). Don't forget to call main() so that you can play the game.

      I think I understand how to make it a step function, but i'm not sure how to do it to implement it into the Guess My Number program in a useful way.

      Any help is appreciated! Thank you!

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        here's a trivial eg of step value
        Code:
        step = 2
        for i in range(0, 100, step):
             print i
        the above will print 0 to 100 in steps of 2. ie 0,2,4,6 etc...

        Comment

        • dshimer
          Recognized Expert New Member
          • Dec 2006
          • 136

          #5
          Ok this is really going to be a stretch, since like you I'm not totally sure what the author is getting at, let me approach if from where I think it is going.

          1) ask_number must improve the program by forcing the new guess to be within the constraints of the highest and lowest known values (if you were keeping track) for example if you find it is lower than 50 on the first guess you constrain future guesses to 0-50 if you find out that it is higher than 25 on the second, the new constraints become 25-50.

          2) By adding a step value (not what I would call it, but I don't even understand what they are asking) you could force the default guess to be a certain number, maybe half the remaining range. In otherwords if the range is 0-100 your guess would step up from the lowest number by the range divided by 2. This isn't actual code but you get the idea.

          Code:
           ask_number(question, low, high,((high-low)/2))
          So if low was 27 and high was 45 the default guess would come up 36

          Comment

          • Cjllewey369
            New Member
            • Mar 2007
            • 14

            #6
            Well, i think that I'm going to tell my "advisor" for this Independant Study that the instructions aren't clear enough and that it doesn't make sense to me and instead I'll just go on to the next chapter. I know how i could adapt this to use in another program that it would actually help in, but that isn't what it asks. Thank you for the help, though. I have learned something, but I still can't read the authors mind. I'll work on it though!

            Thanks

            Comment

            Working...