Python for Absolute Beginners Transcripts
Chapter: Code that interacts with users
Lecture: Exit early on a win

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's put my little hack back. So we know exactly when we're going to win. We've got our back and forth with the user already working
0:08 and that's super cool. The program is taking the data and then it's like having basically a conversation with the user. That's super cool.
0:15 But what's not cool is when I come down here and take a guess and then I get it right it won't just say, cool, here's a menu, go order.
0:23 It keeps asking us exactly five times. So, there's couple of things we can do here. One, you look at what's happening so far we could take this
0:33 and we could kind of use this in like, not the way it's meant to be. We could say that's equal to the attempt of limit. There, like so. Now, by the way
0:46 notice there's a little two spaces I got accidentally here and this says there should be a space. Maybe there's a bunch of lines
0:52 and I wanted to clean up this code PyCharm has this cool thing where you can go code, and say, reformat code
0:58 Command + Alt + L, or you can just hit, Command + Alt + L and it'll clean up the code. So you'll see me reformat code all the time to make it nicer.
1:06 Okay so we could do this and technically this will work. I'll say 10 oh, not like that. I'll say 10 and then I'll say 7 wooh, you're out.
1:16 But there's just something a little bit ick about this. What if down here, we want to say something like you're done in however many attempts, right?
1:25 We want to print that out. Well, if we come down here and get it and you're done in 6. No I wasn't, I was done in one!
1:33 So, there's just some kind of weirdness like we're kind of abusing what this is for and the reason I bring this up is Python has a special keyword
1:41 let's say, I don't care what you're doing in the loop I don't care what this test is just stop. Just leave the loop and just go. We're done.
1:48 We're done with the loop right now. And it's super easy you just say, break. And then you say, break and this just means you come down here
1:54 and then as soon as you get to that you just, you don't ever do anything with the loop again you just exit out and you just pick up right on line 27
2:00 which just says this. So let's test that one more time. 10, no wasn't 10. If I take seven, this should do it, and it should exit out. Boom. Got it.
2:11 And you're done in some number of attempts. It looks like we've got a small ordering issue if we're going to print out the number of attempts.
2:17 So let's put that right up there one more time. There we go. You're done in two attempts. Perfect. This is working.


Talk Python's Mastodon Michael Kennedy's Mastodon