[Tutor] Simple python help needed
Mats Wichmann
mats at wichmann.us
Thu Aug 29 19:13:42 EDT 2019
On 8/29/19 1:19 PM, Cheyne Skapyak wrote:
> Hello, so i'm trying to modify some code so that basically i can choose
> what difficulty of guess the number i want to play. I'm trying to use def
> but it keeps coming back saying they aren't defined which i think they are,
> i cant seem to find a solution. here is the code
after a glance, consider these notes:
you seem to be writing three functions for the three difficulty levels.
you shouldn't need to do that: good function design would mean you can
pass in what you need to know (like the range from which the number is
chosen - you've used 15, 20 and 50); the code is otherwise essentially
common, which indeed makes it a good candidate for a function.
but the functions you've written are really short (one-liners), and the
code that seems to belong to the functions is actually outside them...
in Python that's an indent thing: indentation tells the interpreter what
lines of code belong to a particular block, so everything that belongs
inside
def foo():
needs to be indented to be considered part of that clode block.
And then as has already been noted, the order of things matter. Unlike
some languages, a function definition is an executable "statement"
(usually a whole block of code, see above) that is executed as it is
seen, and the result is a callable object which then can be called by
subsequent code. So just keep that in mind.
More information about the Tutor
mailing list