Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: C# function basics
0:00 The next area that I want to focus on for you is functions.
0:04 So, we've already really sort of
0:06 been using functions in Python.
0:08 You saw we've had our main method and our some_method
0:12 and took an argument, but I want to focus specifically
0:14 on what are the parameters, what can we do
0:17 in terms of flexibility, return values
0:20 and all sorts of stuff like that.
0:21 Method overloading, and so on.
0:24 So we're going to write, at the first start exploring this
0:27 we're going to write a little game, it'll be a lot of fun.
0:29 And then we're going to come back and focus on
0:31 specifically on, method overloading type of operations.
0:36 So here we have a program in C#.
0:38 And let's just run it, I think you'll enjoy.
0:42 It's a high low game, so it says I'm thinking
0:45 of a number between one and 100, how many steps
0:47 can you guess it in, what number am I thinking of?
0:50 This is where you apply your binary search algorithm
0:52 50 too low, 75 too low
0:55 90 too high, 85 too high
0:59 80, 76, 77, 78, 79 it must be.
1:07 There it is, of course, I was thinking of 79.
1:09 Well, my little binary search didn't work that well
1:11 and I kind of broke down there when I got to the 70s
1:13 but nonetheless, we're going to write this program in Python
1:17 it does things like go and get the guess from the person
1:20 and so on. So, it should be a lot of fun
1:22 let's just really quickly look at the code here
1:24 before we go talk about it in Python.
1:27 So here's our main method.
1:30 We've run it and it says ShowHeader
1:32 that's the big little printed bit at the top there
1:34 that says, Welcome to the High Low Game.
1:36 We create a random number generator and we get
1:39 a random integer between one and a hundred.
1:42 And then we also keep track of how many times this happened.
1:45 So when I go through, want to get a guess
1:47 if they don't give us a guess
1:49 it's a knowable editor, if they don't give us a guess
1:51 then we could just tell them, hey, no, you've got to give us
1:54 a number, ask 'em again, something like that.
1:56 Count how many times they give us real guesses
1:58 and evaluate the guess, right here we're passing
2:00 the value of that knowable type in the number
2:03 and then if they get it right they break
2:04 and we say, good job, you did this many times, all right.
2:08 So pretty standard C# down here, easy six lines
2:12 for what it's worth.
2:13 We'll want to come back and look at this in Python as well.
2:15 All right, so this is the game that we're going to recreate
2:19 over in Python to explore working with the functions.