Python for Absolute Beginners Transcripts
Chapter: Code that interacts with users
Lecture: Exit early on a win
0:00 Let's put my little hack back.
0:01 So we know exactly when we're going to win.
0:04 We've got our back and forth with the user already working
0:07 and that's super cool.
0:08 The program is taking the data
0:10 and then it's like having
0:11 basically a conversation with the user.
0:13 That's super cool.
0:14 But what's not cool
0:16 is when I come down here and take a guess
0:17 and then I get it right
0:19 it won't just say, cool, here's a menu, go order.
0:22 It keeps asking us exactly five times.
0:25 So, there's couple of things we can do here.
0:27 One, you look at what's happening so far
0:31 we could take this
0:32 and we could kind of use this
0:34 in like, not the way it's meant to be.
0:37 We could say that's equal to the attempt of limit.
0:43 There, like so.
0:44 Now, by the way
0:45 notice there's a little two spaces I got accidentally here
0:47 and this says there should be a space.
0:49 Maybe there's a bunch of lines
0:51 and I wanted to clean up this code
0:53 PyCharm has this cool thing
0:54 where you can go code, and say, reformat code
0:57 Command + Alt + L, or you can just hit, Command + Alt + L
1:00 and it'll clean up the code.
1:01 So you'll see me reformat code all the time
1:03 to make it nicer.
1:05 Okay so we could do this
1:06 and technically this will work.
1:09 I'll say 10
1:10 oh, not like that.
1:11 I'll say 10 and then I'll say 7
1:13 wooh, you're out.
1:15 But there's just something a little bit ick about this.
1:18 What if down here, we want to say something like
1:21 you're done in however many attempts, right?
1:24 We want to print that out.
1:28 Well, if we come down here and get it
1:29 and you're done in 6.
1:30 No I wasn't, I was done in one!
1:32 So, there's just some kind of weirdness
1:34 like we're kind of abusing what this is for
1:36 and the reason I bring this up
1:38 is Python has a special keyword
1:40 let's say, I don't care what you're doing in the loop
1:42 I don't care what this test is
1:43 just stop.
1:44 Just leave the loop and just go.
1:46 We're done.
1:47 We're done with the loop right now.
1:48 And it's super easy
1:49 you just say, break.
1:50 And then you say, break
1:51 and this just means you come down here
1:53 and then as soon as you get to that
1:54 you just, you don't ever do anything with the loop again
1:57 you just exit out
1:58 and you just pick up right on line 27
1:59 which just says this.
2:01 So let's test that one more time.
2:03 10, no wasn't 10.
2:04 If I take seven, this should do it, and it should exit out.
2:08 Boom. Got it.
2:10 And you're done in some number of attempts.
2:11 It looks like we've got a small ordering issue
2:14 if we're going to print out the number of attempts.
2:16 So let's put that right up there one more time.
2:20 There we go.
2:21 You're done in two attempts.
2:22 Perfect. This is working.