You can find my video here:
Here’s an example:
As you can see, at the end of the program it asks to the user if he/she want to try with another number or ends.
Nice, right?
]]>One way to have data in your code is to get it from external sources such as databases, another computer, the Internet, etc. The options are endless(sort of). Depending on what your code is about you will find that user input is quite valuable if you know what to do with it, we shall see more about this later on. First, we need to see how to get it.
Python makes this super easy for you because it has a built-in function which is unsurprisingly called input.
To declare an input you just add a parenthesis and type your prompt. In the example below I ask for someone’s age or you can get more complex data by creating lists or dictionaries with the input.
n = input(“Please enter your age: “)
The computer kind of replaces “Please enter your name:” with whatever the user writes, which is a prompt string. And when the function is evaluated the input is shown. Remember that whatever the user types, even if it is a number or a character, will ultimately be a string. If you want to use it in any other way then you would use other functions such float or int to modify it.
So in the code above, the variable n is replaced by whatever the user inputs, according to my example if I were to use my short program n will become my age as a string. The next piece of code shows you how to convert it into an integer using the int function.
n = input(“Please enter your age: “)
age = int(n)
I simply created a new variable called age and the value of my variable is mainly the user input but the function int before the name of the variable changes my age as a string intomy age as an integer. Allowing me to work with it.
So what happens if the user input does not match what you’re asking for? You validate it using =.
n = input(“Please enter your age: “)
if n == int:
print(“You look younger!”)
else:
print(“Not a number!”)
Conditionals are useful for this. My code above validates their input by asking whether n is an integer if it is not it prints “not a number” and if it is an integer it prints a very fake compliment.
]]>Here’s the video:
]]>
That means that you can restrict the user to type something no longer than 10 characters, or to type only an integer or a float. Here I going to show you those three cases (something no longer than ten characters, an integer, and a float).
So let’s start with the easiest one; to restrict the number of character the user can type, it is easy to use the length command.
len(variable)
it will return the number of characters in case of a string, the number of elements it has in case of a list.
REMEMBER, in order to do this, the variablke must be either a string or a list, otherwise it won’t work
With a loop with the condition of what the user is typing is longer than ten characters, you can r4estrict the interaction of the user and don’t let him go until he types what you want
So for the program we will ask a nickname no longer than ten characters, the loop after asking the nickname will be:
So it will loop untill the user types it correctly
Then, we will review a little harder one; to restrict the user to only type an integer; that means the input won’t have a “.” inside of it, and it can be converted to integer via python.
There exists a common way to validate that which is by exceptions in a loop. With a loop that it’s only condition is to be true, you will use the command try, to, well, try to do something, in this case, to convert the input of the user to integer via python, variable = int(variable); if it results like an error, represented in python as ValueError, then it will ask again for the number. This loop is to make sure the user didn’t input letters, just numbers.
After doing that, is necessary to verify if the input has a decimal point”.”, in that case, it will ask again for the number
So it will look like this
The last one, the float, its a little more ease, it just have to work if you try to convert it to float; you will use the same structure, but you will try to convert it to float instead to int variable = float(variable)
It will result like this:
Here you saw not just how to validate, but learned the basic types of data
The strings, that are just characters
The integers, that are just numbers, not fractions, not decimals
The floats, that are numbers with decimal points
It is easy isn’t it?
]]>The program will only start to work if it is a positive integer number, otherwise (if it is a string or a negative number) it will ask the user for the number repetitive times until the program receives a positive integer number
]]>#Mastery29 :
Have a good day !
]]>