WSQ06 – Randomising randomness

Hello, people. We’re feeling random today. Random as in python’s random library. Think about libraries as groups of functions that python doesn’t always have at hand. You see, when you use python, your code may have many purposes and python is no medium to tell which functions you are going to use, you actually can do a lot with just python’s basic functions but for example, if you’re working on a  math-related code you might as well import the math library, if you’re just generating random numbers or using a specific distribution, then you should import the random library, as we’re doing today :

————————————————————————–

Editor :

Screen Shot 2016-02-13 at 12.40.18 PM

————————————————————————–

Now we can use any of the functions included in the random library. You might be asking what kind of wicked code is it that we’re working on today, but it’s not that crazy, and not hard at all. Today’s code just needs to pick a random integer from 1 to 100, then ask for a user input from 1 to 100 to make the user guess the random generated number :
————————————————————————–

Editor :

Screen Shot 2016-02-13 at 3.57.51 PM

————————————————————————–

Notice that every time you use a function from a library you’ve imported, you need to type the name of a library and a period before calling the function. There’s a way to avoid this which is to import a specific function from any library, for example :

from random import randint 

In this case, you wouldn’t need to type “random.” in your current code, but it is recommended to use the first method, as it lets you remember, and anyone know, where each function was imported from. 

Anyways, back on our practice, this code let’s the user play only one time. Maybe we’d like the user to play until he’s got the number

Screen Shot 2016-02-13 at 3.55.19 PM
Screen Shot 2016-02-13 at 3.30.14 PM
Screen Shot 2016-02-13 at 3.32.40 PM

————————————————————————–

Editor :

Screen Shot 2016-02-13 at 3.55.19 PM

————————————————————————–

Beautiful. Now the user can guess as many times as he likes… The while loop repeats whatever is inside it only when the condition set is true. When the condition stops being true, as in w = 1, and not 0, the loop stops. I’m starting to think the game is still a little to hard. Maybe we should tell the user if the number is higher or lower than  what he’s guessing :

————————————————————————–

Editor :

Screen Shot 2016-02-13 at 3.30.14 PM

————————————————————————–

Even more beautiful. Now the user will know if he’s guessing to high or too low. If you’re wondering what elif means, you probably can tell that it is a middle point between an if and an else. If the first condition isn’t met, the code turns to the elif for a second condition. When neither the conditions of the if nor elif are met,  else says what must be done. In short, if the number isn’t equal, then elif check it isn’t lower, and finally else, it has to be higher, therefore we needn’t turn in any more conditions, an else is more than enough. 

Now that we’re done, shall we play?…

————————————————————————–

Terminal :

Screen Shot 2016-02-13 at 3.32.40 PM

————————————————————————–

Great! Now the code works perfectly. And with this, we’re done for today.

 This post is for my #WSQ06 on the programming fundamentals course.

CC BY-SA 4.0 WSQ06 – Randomising randomness by esenombredeusua is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.