A look into Python: Working with INPUT function

USING INPUT 

One way a programmer may ask users to give data is by directly asking them to type with the keyboard. This can be done by using input function in Python. When this function is run, Python will ask the user to type something and this information will be saved as strings. A simple example of using input is shown as followed:

As you can see, the codes begin by assigning an input with a variable called name, which is a string. Then, the programmer can ask anything (data) that is required as long as it is in parentheses. In this case, it is asking the name of the user. Once the user has given the answer, the print statement is used to greet the user by calling his name, which is the input from the first question.

OK, now lets make it more interesting. Lets make a simple game in Python by using input. For this game, we will ask the user to match the number's chosen by the program. To make it simple, the number is a randomly generated integer which is in the range from 1 to 5. This means, the user will only need to guess whether the number is either 1, 2, 3, 4 or 5. 

In order to generate a random integer number, we need to import random module as the capability to generate random number is not built-in within the default setting. This can easily be done by typing:

import random

Since random module has many functions in it, we will specifically choose randint within the random module in order to perform this specific task, which is to generate random integer.

Apart from the random module and input function, we also need to use if statement in our code to check and compare the random generated number with the user's input.. To check on this, we either use  == or != symbols. The explanation for this symbols are as followed:

== means equal to     i.e>>> 2==2 will gives True
!= means not equal to i.e >>> 3!=2 will gives True

OK, now lets look at the game's code. Hopefully it's not that confusing. Here it is:

Notice that i have put str in the 2nd line of the code. By doing this, we  are changing the type of a variable. In this case, I'm changing an integer to become a string. I'm doing this because we will be using strings in the print statement and input function, where mix up of variables type will give error message. Hence, i'm converting the integer to a string to avoid any issue in the program. 

Now, lets run the file. 

Referring to the code above, the first line of code is asking the user to pick up a number that ranges from 1 to 5. The user then chooses number 5. Since the number 5 is not equal to the randomly generated number by Python, the user is replied with a sorry statement along with the correct number.

Now, lets look what happens when the user got it correct:

As you can see from above, when the user picks up the correct number, he/she will be greeted with a compliment. 

I do realise that this is just simple example/game. The intention here is to show how the INPUT function can be used to collect data.

Hopefully, this post will allow people to understand more about Python and working with the INPUT function.

Do let me know what do you think on this post. Feel free to comment on anything. I'm more than glad to hear about it.

Cheers,

@rahim.rahman

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center