Re: [Tutor] Help with Guess the number script

2014-03-07 Thread Alan Gauld

On 07/03/14 05:30, Scott W Dunning wrote:

I am trying to write a script for class for a game called guess the number.


Others have given general hints here are a couple of specifics...


def print_hints(secret, guess):
 if guess < 1 or guess > 101:


As I recall the spec said guesses could be between 1 and 100?
What happens above if the guess is 101?


 elif guess < (secret - 10) or guess > (secret - 10):


Do you really mean to test for secret minus 10 in both
expressions?


 elif guess < (secret - 5) or guess > (secret - 5):
 print "You are warmer!"


And similarly here, do you really want to test
against (secret-5) in both cases?


 elif guess < (secret - 10) or guess > (secret - 10):
 print "You are cold!"


And again...


 elif guess < (secret - 5)or guess > (secret - 5):
 print "You are warmer!"


And again...

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] c++ on python

2014-03-07 Thread Alan Gauld

On 07/03/14 14:29, Gabriele Brambilla wrote:


in the next days  I will receive a c++ code that I would like to run in
python (http://docs.python.org/2/extending/index.html).


What do you mean?
Are you receiving C++ source code? If so is it for an
executable program, a library or just a single object file?

When you say run it in python do you mean you want to
launch the executable program from Python (see subprocess
module) or call functions in the library (maybe ctypes)
or do you want to to embed the C code as a Python module?

If its an executable to be run via subprocess thats fine
for this list.

If its using ctypes to call functions in a library thats
about the extreme edge of this list.But there is a ctypes
list that is probably more appropriate.

If its embedding the C library as a python module that
is definitely off topic and you should probably try the
main Python list


It should be self consistent (no extraroutines).


Again I'm not sure what you mean by that.
Do you mean it has no dependencies on other code - not
even glibc? Or just that it is a standalone executable?


I want to be ready to use it... Has someone some C++ code examples
available that I can try to run easily before getting that code?


You need to be a lot more specific about what you mean.
What exactly are you trying to do?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] c++ on python

2014-03-07 Thread eryksun
On Fri, Mar 7, 2014 at 9:29 AM, Gabriele Brambilla
 wrote:
>
> in the next days  I will receive a c++ code that I would like to run in
> python (http://docs.python.org/2/extending/index.html).
> It should be self consistent (no extraroutines).
> I want to be ready to use it... Has someone some C++ code examples available
> that I can try to run easily before getting that code?

Writing CPython extension modules is probably off topic for
python-tutor. ctypes is on topic, but manually wrapping a C++ library
with a C API that's accessible via ctypes is a lot of work. I
recommend using SWIG or Cython instead.

http://cython.readthedocs.org/en/latest/src/userguide/wrapping_CPlusPlus.html

cython-users forum:
http://groups.google.com/group/cython-users

http://swig.org/Doc2.0/Python.html#Python
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] c++ on python

2014-03-07 Thread Gabriele Brambilla
Hi,
in the next days  I will receive a c++ code that I would like to run in
python (http://docs.python.org/2/extending/index.html).
It should be self consistent (no extraroutines).
I want to be ready to use it... Has someone some C++ code examples
available that I can try to run easily before getting that code?

thanks

Gabriele
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-07 Thread spir

On 03/07/2014 06:30 AM, Scott W Dunning wrote:

I am trying to write a script for class for a game called guess the number.  
I’m almost done but, I’m having a hard time getting the hints to print 
correctly.  I’ve tried ‘if’’ ‘elif’ nested, it seems like everything….I’m 
posting my code for the hints below, any help is greatly appreciated!

def print_hints(secret, guess):
 if guess < 1 or guess > 101:
 print
 print "Out of range!"
 print
 if guess < secret:
 print
 print "Too low!"
 elif guess < (secret - 10) or guess > (secret - 10):
 print "You are cold!"
 print
 print "Please play again!"
 elif guess < (secret - 5) or guess > (secret - 5):
 print "You are warmer!"
 print
 else:
 print "You're on fire!!"

 if guess > secret:
 print
 print "Too high!"
 print
 elif guess < (secret - 10) or guess > (secret - 10):
 print "You are cold!"
 print
 elif guess < (secret - 5)or guess > (secret - 5):
 print "You are warmer!"
 print
 print "Please play again!"
 else:
 print "You're on fire!!"


Thanks again!!

Scott


You are providing 3 kinds of hints to the player, and trying to mix 2 of them 
_cleverly_ in code, which leads to confusion. Much more clever in fact, in 99% 
cases, not to try to clever, because programming is difficult enough; 
programming provides us with high challenges to your limited mind, whether or 
not we add to the difficulty with supposed clever tricks.


clever programming is stupid
stupid programming is clever

The three kinds of hints are:

* whether or not the guess is in interval; this must be done first, and apart, 
which you do well; but when it is not the case, you can quit the function 
immediately: the other tests & honts make no sense; right? challenge: update 
your design so that the program tells whether the guess is in _present_ 
interval, instead of the _initial_ one


* too high, too low, or found (and "on fire"); in the latter case, you can quit 
the function


* whether "cold", "warmer", or in-between; this only makes sense if guess is in 
interval, and not found; note that in fact depends on the _absolute_ difference


Another logic is to reverse the last two hints: first deal with the 
"temperature" hints, including "on fire"; then only say high/low, when in 
interval and not found / not "on fire". I guess this more corresponds to what 
you were trying to express.


d




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine which function code is being called from

2014-03-07 Thread eryksun
On Fri, Mar 7, 2014 at 6:45 AM, Danny Yoo  wrote:
>
> Although many of the recommendations have been discouraging the stack
> inspection approach, even stack inspection might be appropriate,
> though it's certainly not a technique for beginners.

Stack inspection is really intended for debugging or logging. The
stack frame functions in the inspect module really shouldn't be used
for program logic. The warning in the docs about inspect.currentframe
applies equally to inspect.stack. These functions rely on
sys._getframe, which is a CPython implementation detail.

PyPy and Jython do support sys._getframe. IronPython 2.6 added the
command-line options -X:Frames and -X:FullFrames to support
sys._getframe, but its design using the DLR doesn't require frame
objects. Creating them is extra work and probably degrades performance
(definitely for FullFrames).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine which function code is being called from

2014-03-07 Thread Danny Yoo
> def funcB(runfromB):
> funcA(runfromB=runfromB)
>
> funcB(runfromB=True)



Yes, this is something that might be appropriate.  But it really does
depends on your context.  Although many of the recommendations have
been discouraging the stack inspection approach, even stack inspection
might be appropriate, though it's certainly not a technique for
beginners.

That's why I think we need your context.  Can you say more about why
you're trying to do this?  You must be thinking of something more than
the funcA and funcB examples above.  Can you give us a scenario where
you want to do this?  It's an unusual request, so we'd like to know
more.


Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine which function code is being called from

2014-03-07 Thread Jignesh Sutar
Hi All, thanks for the comments and yes I am a tad confused and a little
out of my comfort zone with what I am trying to achieve here. Is this below
any better way (or even worse?) of trying to achieve this. Though this
doesn't actually achieve what I want it to, it may nonetheless highlight
the concept in principle?

def funcA(runfromB=False):
if runfromB is False:"running from funcA" # print only if running from
funcA
print "running from funcA or funcB" #print when running from either
function
if runfromB is True: "running from funcB" # print only when running
from funcB

def funcB(runfromB):
funcA(runfromB=runfromB)

funcB(runfromB=True)


On 6 March 2014 20:37, Jerry Hill  wrote:

> On Thu, Mar 6, 2014 at 12:00 PM, Jignesh Sutar  wrote:
> > Hi I'm trying to exclude a certain line of code if the function is
> called by
> > another function, see illustration below:
>
> As other have said, this is not often a good idea.  That said, it is
> possible to inspect the call stack to see what called a particular
> function, like this (python 3.3):
>
> iimport inspect
>
> def funcA():
> caller = inspect.stack()[1][3]
> print('funcA was called by ' + caller)
> if caller == '':
> print("running from funcA")# print only if running from funcA
> if caller in ('', 'funcB'):
> print("running from funcA or funcB") # print when running from
> either function
> if caller == 'funcB':
> print("running from funcB") # print only when running from funcB
>
> def funcB():
> funcA()
>
> print('- Calling funcA() directly -')
> funcA()
> print('- Calling funcB() -')
> funcB()
>
> Output:
>
> >>>
> - Calling funcA() directly -
> funcA was called by 
> running from funcA
> running from funcA or funcB
> - Calling funcB() -
> funcA was called by funcB
> running from funcA or funcB
> running from funcB
> >>>
>
>
> --
> Jerry
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-07 Thread Peter Otten
Scott W Dunning wrote:

> I am trying to write a script for class for a game called guess the
> number.  I’m almost done but, I’m having a hard time getting the hints to
> print correctly.  I’ve tried ‘if’’ ‘elif’ nested, it seems like
> everything….I’m posting my code for the hints below, any help is greatly
> appreciated!
> 
> def print_hints(secret, guess):
> if guess < 1 or guess > 101:
> print
> print "Out of range!"
> print
> if guess < secret:
> print
> print "Too low!"
> elif guess < (secret - 10) or guess > (secret - 10):
> print "You are cold!"
> print
> print "Please play again!"
> elif guess < (secret - 5) or guess > (secret - 5):
> print "You are warmer!"
> print
> else:
> print "You're on fire!!"
> 
> if guess > secret:
> print
> print "Too high!"
> print
> elif guess < (secret - 10) or guess > (secret - 10):
> print "You are cold!"
> print
> elif guess < (secret - 5)or guess > (secret - 5):
> print "You are warmer!"
> print
> print "Please play again!"
> else:
> print "You're on fire!!"

You are trying too much at once. Start with a simple

def get_high_low_hints(secret, guess):
# buggy; I trust that you can fix it yourself.
if guess < secret:
return "too high"
elif guess > secret:
return "too low"
else:
assert secret == guess
return "correct"

You can then write simple tests like

assert get_high_low_hints(secret=99, guess=99) == "correct"
assert get_high_low_hints(secret=50, guess=51) == "too high"
assert get_high_low_hints(secret=7, guess=6) == "too low"

(which will fail btw, I added a bug to demonstrate the power of this 
approach) or have a look at the unittest module if you are ambitious. Once 
you have fixed the bugs in get_high_low_hints() you can take the next step

def get_close_far_hints(secret, guess):
... # your code; have a look at the abs() function

assert get_close_far_hints(50, 0) == "cold"
assert get_close_far_hints(50, 90) == "cold"

assert get_close_far_hints(50, 59) == "warm"
assert get_close_far_hints(50, 41) == "warm"

... # similar tests for "hot"/"on fire"

In short: break you problem in small sub-problems that you can easily test. 
It may look like it is more work at first, but you are more likely to reach 
a robust working result soon.

Note that my functions do not print anything. This is be because functions 
that don't print are much easier to test:

assert function_that_returns_something() == expected_result

versus

start_capturing_stdout()
function_that_prints_something()
stop_capturing_stdout()
assert get_captured_stdout() == expected_result


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-07 Thread Danny Yoo
One more note:  I used round parenthesis in the diagrams above to
indicate intervals.  If you think about it a bit, you'll realize I
should be using square brackets in some places, or make some
distinctive graphic.  Open and closed circles, perhaps?

Otherwise, there are tiny pinpoint gaps in the case analysis where
things leak through.  You want the case analysis to be watertight, not
like a sieve.  :P
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-07 Thread Ben Finney
Scott W Dunning  writes:

> I am trying to write a script for class for a game called guess the
> number.  I’m almost done but, I’m having a hard time getting the hints
> to print correctly.  I’ve tried ‘if’’ ‘elif’ nested, it seems like
> everything….

And, what happens? Please describe what you do (in enough detail so that
we can try it too), what you're expecting to see, and what happens
instead.

-- 
 \  “It is the integrity of each individual human that is in final |
  `\examination. On personal integrity hangs humanity's fate.” |
_o__)   —Richard Buckminster Fuller, _Critical Path_, 1981 |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-07 Thread Danny Yoo
Can you split the conditions so that they're not overlapping?

One of the things that you learn about programming is that if the
program is hard for humans to read, even if the program is computing
something useful, you may want to work to make it humanly
understandable, even if the human is having a bad day.  :P

That is, if you're doing something like:

if A: ...
if B: ...
elif C: ...
elif D: ...
else: ...
if E: ...
elif F: ...
elif G: ...
else: H: ...

which is, roughly, the control flow within your program, then it
should not be too surprising that this is unexpectedly difficult to
understand.  I don't understand it.


Can you try to rephrase what you're doing as a bunch of
_nonoverlapping_ alternatives?

That is, just look at the conditions your program has now.

#
if guess < secret:
...
elif guess < (secret - 10) or guess > (secret - 10):
...
elif guess < (secret - 5) or guess > (secret - 5):

#

I can't tell, from a glance, that these conditions aren't overlapping.
 Wait.  In fact, I'm pretty sure they are overlapping.

Concretely, if guess is 0 and secret is 11, then the first, second,
and third cases are all true.

This is a sign that the case analysis is fragile.



To get it right: draw it out graphically.  It will help.  You are
trying to break down the number line into sections.


   ---secret-

where you want some condition to capture warmness:


Case A:

   -()secret()



You want another to capture not-so-warmness


Case B:

   (---)--secret--(---)---


one for the too-low case:

Case C:

   ---)---secret--


one for the too-high case:

Case D:

   ---secret---(--


What's case have we forgotten?  The winning one: the one where the
secret and the guess are the same!  Call that Case E.


Ok, that should cover it all, right?  Notice that these are _five_
distinct, non-overlapping cases, and it should be exhaustive.  For any
guess and secret, exactly one of the cases above will apply.  And
because the conditions don't overlap, it won't even matter what order
you check for A, B, C,  D, or E.


You might be able to break this down into a different case analysis.
But try doing the non-overlapping approach.  It should be clear that,
if you express it well, it won't break on you.



If you have more questions, please feel free to ask.  Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with Guess the number script

2014-03-07 Thread Scott W Dunning
I am trying to write a script for class for a game called guess the number.  
I’m almost done but, I’m having a hard time getting the hints to print 
correctly.  I’ve tried ‘if’’ ‘elif’ nested, it seems like everything….I’m 
posting my code for the hints below, any help is greatly appreciated!  

def print_hints(secret, guess):
if guess < 1 or guess > 101:
print
print "Out of range!"
print
if guess < secret:
print
print "Too low!"
elif guess < (secret - 10) or guess > (secret - 10):
print "You are cold!"
print
print "Please play again!"
elif guess < (secret - 5) or guess > (secret - 5):
print "You are warmer!"
print
else:
print "You're on fire!!"

if guess > secret:
print
print "Too high!"
print
elif guess < (secret - 10) or guess > (secret - 10):
print "You are cold!"
print
elif guess < (secret - 5)or guess > (secret - 5):
print "You are warmer!"
print
print "Please play again!"
else:
print "You're on fire!!"


Thanks again!!

Scott
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor