This video belongs to the openHPI course Programmieren lernen mit Python. Do you want to see more?
An error occurred while loading the video player, or it takes a long time to initialize. You can try clearing your browser cache. Please try again later and contact the helpdesk if the problem persists.
Scroll to current position
- 00:00We'll move on to the next video.
- 00:02Today we want to show you a more complex example.
- 00:06There's supposed to be a little game at our party, where the computer thinks up a number between 1 and 100 and the participants at the party are then supposed to guess them.
- 00:16They receive feedback, for example, that the number they guessed was too large.
- 00:21What does the program flow look like now?
- 00:24So the first thing our computer does is think of a number, then a user can enter a number.
- 00:31This is then checked and we receive feedback, whether the number is too large or too small.
- 00:37And if that's the case, then we want to enter a new number again.
- 00:40And so we jump back up into the program sequence, where the user can enter a number.
- 00:46This all sounds very complex right now, but you already know most of it.
- 00:51And you know how to generate a random number, that the computer is, so to speak, making up.
- 00:56And you can read an input from the user and convert it into a number.
- 01:01You can also compare the two numbers then, and you also know how to intercept invalid input from the user.
- 01:09Of course, the last thing you know is, as you keep repeating the whole thing until we find the number.
- 01:14So let's just start with the first step, that our computer will come up with a number.
- 01:19Therefore we import the function randint from the library random and then think of a random number between 1 and 100 in line 2
- 01:29and store them in the variable number and then spend afterwards that we have now come up with a number.
- 01:37The next thing we want to do is to ask the user, to enter a number between 1 and 100.
- 01:42This works again with the input function.
- 01:45The result of this is stored in the input variable, but it is currently still a string, which we first have to convert into a number, and then compare it with the number you made up.
- 01:56This can be done with the help of the function int.
- 01:59So if we run the whole thing, we'll only have the output: "I came up with a number" and we can put in a number.
- 02:05However, nothing else is happening yet.
- 02:07Exactly, because to do that, we first have to enter the number with the one the computer came up with.
- 02:15So in the if statement, we check if our entered number is the same as the one we entered, that the computer came up with.
- 02:22And if that's the case, then we spend: "That was my number. Congratulations".
- 02:27If this is not the case, the elif is checked, namely whether our entered number is less than the number that the computer thought up.
- 02:35In this case the output is then, that our number is unfortunately too small.
- 02:39And if neither of these is true, then the rest is done, so then our input number is too large.
- 02:46So now we can enter a number and also receive feedback on whether our number was too large or too small.
- 02:53But what happens if the user makes a wrong entry, for example, the string "hello"?
- 03:00In that case, we would try to convert the "Hello" into a number with the function int, where a value error occurs, as we learned in the last video.
- 03:08In order to intercept the whole thing, we can add error handling again, that is, we pack the part from line 6 to line 12 into a try
- 03:17and intercept that a value error may occur and then output that the entry was not valid.
- 03:23Right. But all we can do so far is enter a number and don't keep guessing until we get the number the computer thinks it is.
- 03:32So we actually want this part to be repeated over and over again, until we find the number.
- 03:39Therefore we use a while loop and a new variable found.
- 03:44So this is where you check whether found is equal to false.
- 03:48If this is the case, then the part below will be executed.
- 03:52So in the beginning the variable is False, because we haven't found our number yet.
- 03:57And only within the loop, when we have discovered the right time, this variable is set to True.
- 04:04The next time the condition of the loop is checked again, then the result of the condition is False, because our variable found is now true.
- 04:15And so the program is then finished.
- 04:18So we have written our finished program, and the participants at the party can now try their hand at guessing numbers.
- 04:26But let's look again, what the whole thing really looks like.
- 04:29First of all, we have the issue that I came up with a number and can enter a number between 1 and 100.
- 04:35For example, if we enter 50, we get feedback that it was too big.
- 04:39On the other hand, if we try to enter a text like "Hello", then we get the output that the input was invalid.
- 04:47However, the program does not abort, but we can keep on guessing.
- 04:50For example, if we try the 25, we get feedback that it was too small.
- 04:56So our desired number must be somewhere between 25 and 50.
- 05:00How about the 37? If we input that, we get an output that says it was right, and the program is ready.
- 05:08You have to keep in mind that in Code Ocean the performance time is limited, so you can't keep guessing forever.
- 05:16This will protect you from it, that you may have looped back and forth.
- 05:20So we hope you enjoy guessing numbers.
To enable the transcript, please select a language in the video player settings menu.