1 00:00:00,450 --> 00:00:03,150 I'm going to clean up these comments, they're getting a little busy, 2 00:00:03,150 --> 00:00:04,320 these instructional ones. 3 00:00:04,320 --> 00:00:07,260 So we'll get rid of this, so I'm using again, 4 00:00:07,260 --> 00:00:10,590 Command + Shift + D to delete some lines here. 5 00:00:10,590 --> 00:00:12,035 Let's bring this up. 6 00:00:12,035 --> 00:00:16,705 Don't need that anymore, don't need that or that or those. 7 00:00:16,705 --> 00:00:17,837 Let's get rid of that, 8 00:00:17,837 --> 00:00:21,730 I'm going to leave that to-do because we definitely need to do that later. 9 00:00:21,730 --> 00:00:23,500 Get rid of this. 10 00:00:23,500 --> 00:00:25,780 Otherwise, and thanks. 11 00:00:25,780 --> 00:00:27,640 Sold tickets. 12 00:00:27,640 --> 00:00:28,990 This line, there we go. 13 00:00:28,990 --> 00:00:31,240 And it's actually 20 lines of code, awesome. 14 00:00:32,260 --> 00:00:32,950 That feels better. 15 00:00:32,950 --> 00:00:37,410 I'm gonna save it, and now, when we get this cleaned up, 16 00:00:37,410 --> 00:00:39,730 should we walk this real quick with those missing? 17 00:00:39,730 --> 00:00:43,184 Remember, that we are on the, as a user I should have errors reported in 18 00:00:43,184 --> 00:00:46,136 a user-friendly manner, which is kind of always the case. 19 00:00:46,136 --> 00:00:49,440 But we really wanted to make sure that we handled this cuz they saw one of 20 00:00:49,440 --> 00:00:51,180 the tracebacks that I left earlier. 21 00:00:51,180 --> 00:00:53,280 So all right, here we go. 22 00:00:53,280 --> 00:00:53,940 First off. 23 00:00:53,940 --> 00:00:58,530 So here, we have a constant and it's in all upper case, it won't change while 24 00:00:58,530 --> 00:01:02,340 the program's running and if the group wants to change this in the future, 25 00:01:02,340 --> 00:01:06,927 it's a very easy place to change it, constants. 26 00:01:06,927 --> 00:01:11,800 So these are how many tickets are currently available, now note, 27 00:01:11,800 --> 00:01:13,170 it's not all uppercase. 28 00:01:13,170 --> 00:01:15,380 So we can assume that this is going to change. 29 00:01:16,870 --> 00:01:21,715 And then we have, while there is one or more tickets remaining. 30 00:01:21,715 --> 00:01:25,029 Okay, we're going to keep on living through all this code. 31 00:01:25,029 --> 00:01:28,250 So we're going to show how many are left. 32 00:01:28,250 --> 00:01:31,680 Then we're going to grab their info, what's your name, how many tickets, 33 00:01:31,680 --> 00:01:33,730 we're going to coerce that. 34 00:01:33,730 --> 00:01:36,540 Because input always returns a string, but 35 00:01:36,540 --> 00:01:39,890 we need to use num_tickets for a math purpose. 36 00:01:39,890 --> 00:01:42,720 So we definitely need an integer. 37 00:01:42,720 --> 00:01:45,460 Now, it's an integer because you can't buy fractions of a ticket. 38 00:01:45,460 --> 00:01:48,460 I'd like to buy one and a half tickets please, that doesn't work. 39 00:01:48,460 --> 00:01:52,640 So if you remember, this seems a little bit dangerous, doesn't it? 40 00:01:52,640 --> 00:01:56,320 What if the user gave us something that we couldn't coerce? 41 00:01:56,320 --> 00:01:58,790 Let's keep this in mind for a possible problem. 42 00:01:59,865 --> 00:02:02,060 Okay, and then we calculate the price. 43 00:02:02,060 --> 00:02:04,320 And then we show them the total. 44 00:02:04,320 --> 00:02:07,220 And we ask them, yes or no, do you want to proceed? 45 00:02:07,220 --> 00:02:10,440 And because we aren't sure if they are going to use an uppercase Y or 46 00:02:10,440 --> 00:02:13,450 lowercase in responding, we lowercase it. 47 00:02:13,450 --> 00:02:15,040 And we check. 48 00:02:15,040 --> 00:02:20,760 We left a nice to-do message here that the SOLD is not really taking any credit 49 00:02:20,760 --> 00:02:26,659 cards, and then we subtract the number of tickets from the tickets remaining. 50 00:02:26,659 --> 00:02:30,297 What would happen if the user entered 1,000 tickets? 51 00:02:30,297 --> 00:02:34,490 Whoa, that'd be negative 900 tickets remaining. 52 00:02:34,490 --> 00:02:36,710 The while loop up here, this would stop, right? 53 00:02:36,710 --> 00:02:39,090 Cuz it'd be negative 900, that would stop. 54 00:02:39,090 --> 00:02:41,320 It's not greater than or equal to one, but 55 00:02:41,320 --> 00:02:44,980 still, the 1,000 ticket sale would go through. 56 00:02:44,980 --> 00:02:46,550 We've got to catch that, right? 57 00:02:46,550 --> 00:02:49,530 So and then finally, anything other than y, 58 00:02:49,530 --> 00:02:53,200 we're going to say Thank you anyways, and we're going to be friendly about that. 59 00:02:53,200 --> 00:02:56,650 And then we're going to let people know that those tickets are sold out, 60 00:02:56,650 --> 00:02:57,740 because the loop's over. 61 00:02:57,740 --> 00:03:00,160 So once that loop's over, they know. 62 00:03:00,160 --> 00:03:02,630 Did that feel pretty good being able to read most of that? 63 00:03:02,630 --> 00:03:03,606 That's pretty cool, right? 64 00:03:03,606 --> 00:03:05,622 Pretty amazing. 65 00:03:05,622 --> 00:03:08,820 All right, now, let's take care of these errors. 66 00:03:08,820 --> 00:03:10,220 Let's do one at a time. 67 00:03:10,220 --> 00:03:15,350 So the first problem that we have is here, one around coercion, right? 68 00:03:15,350 --> 00:03:17,910 So remember, the user can add anything here. 69 00:03:17,910 --> 00:03:22,620 So let's run it to see how bad the errors actually are. 70 00:03:24,210 --> 00:03:25,320 I want to clear this. 71 00:03:26,600 --> 00:03:33,520 Let's run python masterticket, and our name is Gallahad. 72 00:03:33,520 --> 00:03:35,020 How many tickets would you like? 73 00:03:35,020 --> 00:03:36,480 Blue. 74 00:03:36,480 --> 00:03:39,330 Blah, what a gross error. 75 00:03:39,330 --> 00:03:41,090 And our program ended. 76 00:03:41,090 --> 00:03:42,320 We can't have that happening. 77 00:03:42,320 --> 00:03:45,470 We won't be able to sell anything more if we drop out like that. 78 00:03:45,470 --> 00:03:49,327 So it throws a ValueError. 79 00:03:49,327 --> 00:03:52,190 I'm gonna go ahead, I'm gonna copy that. 80 00:03:55,130 --> 00:03:59,070 And right above here, right above int( num_tickets), let's say expect, 81 00:04:00,240 --> 00:04:03,160 I'm gonna paste a ValueError to happen. 82 00:04:04,370 --> 00:04:07,757 And handle it appropriately. 83 00:04:12,088 --> 00:04:15,010 Let me give you a hint, remember to test it out. 84 00:04:16,270 --> 00:04:17,880 Pause me, and give it a go. 85 00:04:17,880 --> 00:04:18,380 Are you ready? 86 00:04:19,910 --> 00:04:21,070 All right, ready? 87 00:04:21,070 --> 00:04:22,540 This is how I went about fixing it. 88 00:04:22,540 --> 00:04:25,320 So I definitely needed a trial block. 89 00:04:25,320 --> 00:04:29,574 So I did the block here and we wanna be careful about the num_tickets. 90 00:04:33,028 --> 00:04:38,692 And I want to accept, we know that that throws a ValueError. 91 00:04:38,692 --> 00:04:44,560 And what we wanna say is print, 92 00:04:44,560 --> 00:04:49,170 no, we ran into an issue, 93 00:04:49,170 --> 00:04:52,530 please try again. 94 00:04:52,530 --> 00:04:54,160 Now, that feels good, doesn't it? 95 00:04:54,160 --> 00:04:55,970 But I'm gonna go ahead and run this. 96 00:04:57,680 --> 00:05:01,670 My name is Gallahad and I would like. 97 00:05:01,670 --> 00:05:04,000 How many tickets would you like blue. 98 00:05:04,000 --> 00:05:05,710 Total due is blue blue blue blue blue. 99 00:05:06,770 --> 00:05:07,820 What? 100 00:05:07,820 --> 00:05:11,166 How is blue blue blue blue blue. 101 00:05:11,166 --> 00:05:12,100 Yes. 102 00:05:13,530 --> 00:05:17,960 Remember, this is where the error happened and the assignment never happened. 103 00:05:17,960 --> 00:05:21,010 So num_tickets here is the string blue. 104 00:05:21,010 --> 00:05:24,050 And because you can multiply strings, 105 00:05:24,050 --> 00:05:26,900 right, repeat the same thing a number of times. 106 00:05:26,900 --> 00:05:29,640 This repeated however marks the ticket price was which is ten, so 107 00:05:29,640 --> 00:05:32,940 those are ten blues right there. 108 00:05:33,980 --> 00:05:39,000 So in order to fix that, I need to put this code in an else. 109 00:05:40,190 --> 00:05:43,780 Cuz we only want this code to run if we know how many there are. 110 00:05:43,780 --> 00:05:48,995 So let's do this, and I'm going to indent it with command right bracket. 111 00:05:51,665 --> 00:05:55,892 Then let's go ahead, and if you want to ever stop out of a program while 112 00:05:55,892 --> 00:06:00,779 it's waiting at a prompt, you can do a control C and it will send this interrupt. 113 00:06:02,380 --> 00:06:04,380 So let's run that one more time, make sure it's working. 114 00:06:05,610 --> 00:06:07,890 My name is Gallahad, blue. 115 00:06:09,160 --> 00:06:11,740 It says, no, we ran into an issue, and then it just keeps on looping. 116 00:06:11,740 --> 00:06:13,550 There's 100 tickets remaining, awesome. 117 00:06:13,550 --> 00:06:15,810 Okay, now we have another error, too, don't we? 118 00:06:15,810 --> 00:06:18,790 We don't want to allow for that 1,000 ticket sale, right? 119 00:06:18,790 --> 00:06:20,130 We need to block that. 120 00:06:20,130 --> 00:06:23,120 So why don't we do that in the try portion here? 121 00:06:23,120 --> 00:06:27,000 So if we come up here into our try, 122 00:06:27,000 --> 00:06:31,840 we wanna make sure that we have a valid number of tickets to request, right? 123 00:06:31,840 --> 00:06:39,391 So we will raise a ValueError if the request is for 124 00:06:39,391 --> 00:06:44,742 more tickets than are available. 125 00:06:44,742 --> 00:06:48,494 And then I wanna make sure that that's a user friendly error that we raise. 126 00:06:48,494 --> 00:06:55,310 So what I want to do next is I wanna include in the output here, 127 00:06:55,310 --> 00:07:01,290 we want to include the error text in the output. 128 00:07:01,290 --> 00:07:02,140 All right. 129 00:07:02,140 --> 00:07:04,430 Remember, you can lean on the community. 130 00:07:04,430 --> 00:07:07,780 And also, you can rewatch the previous video where we did something similar, 131 00:07:07,780 --> 00:07:09,110 check the Teacher's Nets. 132 00:07:09,110 --> 00:07:10,010 You got this. 133 00:07:10,010 --> 00:07:10,960 Are you ready? 134 00:07:10,960 --> 00:07:11,460 Pause me. 135 00:07:12,800 --> 00:07:13,330 Ready? 136 00:07:13,330 --> 00:07:14,320 So here's what I did. 137 00:07:14,320 --> 00:07:17,140 So I used an if statement up here. 138 00:07:17,140 --> 00:07:21,920 So I said, if the number of tickets, that's what they asked for is greater 139 00:07:21,920 --> 00:07:26,070 than the tickets_remaining, you can't do that, you can't buy those tickets. 140 00:07:27,570 --> 00:07:33,086 So, what we did was we raised a ValueError. 141 00:07:33,086 --> 00:07:37,458 And I'm gonna go ahead and be very specific, there are only, 142 00:07:37,458 --> 00:07:40,750 use a placeholder here, tickets remaining. 143 00:07:43,200 --> 00:07:43,710 Nice try. 144 00:07:45,180 --> 00:07:48,750 So then we'll do format and we'll say tickets remaining, 145 00:07:53,110 --> 00:07:55,100 and then we'll do a closing. 146 00:07:55,100 --> 00:07:58,580 Okay, and then if I want to use that exception, 147 00:07:58,580 --> 00:08:02,030 you have to use the as keyword, did you remember that? 148 00:08:02,030 --> 00:08:05,420 If not, don't fret, we only did that one time in this course. 149 00:08:05,420 --> 00:08:08,500 The more you practice, the more you will be able to recall it. 150 00:08:08,500 --> 00:08:12,048 So, you have to use the as keyword, so as err. 151 00:08:12,048 --> 00:08:18,350 So, no, we ran into an issue, I'm gonna go ahead and drop our message there, 152 00:08:18,350 --> 00:08:21,230 then I'll, was that a period, space, please try again. 153 00:08:22,240 --> 00:08:27,907 And then we will format that with our error, format, err. 154 00:08:27,907 --> 00:08:28,407 There we go. 155 00:08:31,961 --> 00:08:33,844 Okay, I'm going to break out of this, 156 00:08:33,844 --> 00:08:36,197 I'm going to clear this and run it one more time. 157 00:08:38,465 --> 00:08:39,460 What is your name? 158 00:08:39,460 --> 00:08:41,543 It is Sir Robin. 159 00:08:41,543 --> 00:08:46,713 I would like 10,000, no we've ran into an issue, there only 100 tickets remaining, 160 00:08:46,713 --> 00:08:50,710 please try again, there are 100 tickets remaining. 161 00:08:50,710 --> 00:08:57,622 Awesome, so I think we can move this final ticket over. 162 00:08:57,622 --> 00:08:59,882 How good does that feel? 163 00:08:59,882 --> 00:09:03,650 I am gonna go run a demo for Money Python and see what they think, 164 00:09:03,650 --> 00:09:05,610 they're gonna love it, I know they're gonna love it