473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. def ask_number(question, low, high):
  2.     """Ask a yes or no question."""
  3.     response = None
  4.     while response not in range(low, high):
  5.         response = int(raw_input(question))
  6.     return response
  7.  
Thank you for your help!
Apr 11 '07 #1
5 10809
dshimer
136 Expert 100+
Found a reference to the question you are asking at
http://mail.python.org/pipermail/pyt...ch/432313.html
though I can't really see what or why you would even want to step something in the example.
Apr 11 '07 #2
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:

Expand|Select|Wrap|Line Numbers
  1. # Guess My Number
  2. #
  3. # The computer picks a random number between 1 and 100
  4. # The player tries to guess it and the computer lets
  5. # the player know if the guess is too high, too low
  6. # or right on the money
  7.  
  8. import random
  9.  
  10. print "\tWelcome to 'Guess My Number'!"
  11. print "\nI'm thinking of a number between 1 and 100."
  12. print "Try to guess it in as few attempts as possible.\n"
  13.  
  14. # set the initial values
  15. the_number = random.randrange(100) + 1
  16. guess = int(raw_input("Take a guess: "))
  17. tries = 1
  18.  
  19. #guessing loop
  20. while (guess != the_number):
  21.     if (guess > the_number):
  22.         print "Lower..."
  23.         tries += 1
  24.     else:
  25.         print "Higher..."
  26.         tries += 1
  27.  
  28.     guess = int(raw_input("Take a guess: "))
  29.  
  30. print "You guessed it! The number was", the_number
  31. print "And it only took you", tries, "tries!\n"
  32.  
  33. raw_input("\n\nPress the enter key to exit.")
  34.  
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(number 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!
Apr 12 '07 #3
ghostdog74
511 Expert 256MB
here's a trivial eg of step value
Expand|Select|Wrap|Line Numbers
  1. step = 2
  2. for i in range(0, 100, step):
  3.      print i
  4.  
  5.  
the above will print 0 to 100 in steps of 2. ie 0,2,4,6 etc...
Apr 12 '07 #4
dshimer
136 Expert 100+
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.

Expand|Select|Wrap|Line Numbers
  1.  ask_number(question, low, high,((high-low)/2))
So if low was 27 and high was 45 the default guess would come up 36
Apr 12 '07 #5
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
Apr 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
16
by: aarklon | last post by:
Hi folks, recently i read the book named assembly language step by step by Jeff Duntemann. in the chapter coding for linux, he has got a paragraph named C calling conventions, which he...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
5
by: Stinky Pete | last post by:
Hi (again) ;-) I'm still very much at the bottom of a steep learning curve with VB, so any and all help is always appreciated. I've found some code to generate the user names who have logged...
1
by: Memphis Steve | last post by:
Is it possible to combine multiple javascipts into one file and then call that file from a linked URL in the head section of an XHTML file? Here are the two scripts I want to use with the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.