When you write this code, the interpreter will wait until it gets a response from you. Assuming your
age is 45, the program will look like this:
>>> my_age = input (“Please, What is your current age?”)
Please, What is your current age? 45
>>> my_age
“45”
The users enter 45; however, when we request for the value of the age from the interpreter, it returns
“45”, which is an illustration of the string value we inputted. As we already know, Python interprets
the input() function as a string since the number has double-quotes. Notwithstanding, if you decide
to print only the input, that will work well. However, printing the input as a number when it is in
string format will generate an error.
>>> my_age = input (“Please, What is your current age?”)
Please, What is your current age? 45
>>> my_age
40
If
you run this program, you will get an error. This is what you will see.
>>> my_age = input (“Please, What is your current age?”)
Please, What is your current age? 45
>>> my_age >=40
Traceback (most recent call last):
File “”, line 1,
TypeError: unorderable types: str() >= int()
What happened in this code is that you try to use the input() function to perform a numerical
evaluation in line 3. Python generates an error message because you are trying to compare a string
with an integer type. The string in this situation is 45, which is assigned to the variable my_age,
which can’t be compared to the number value of 40.
To solve this issue, we have to use the int() function then informs the Python interpreter to accept it
as a numerical value. Therefore, it then converts the “number string” into a numerical value, as
shown below.
>>> my_age = input (“Please, What is your current age?”)
Please, What is your current age? 45
>>> my_age >=int(my_age)
>>>my_age >= 40
True
In the code above, when prompted to enter how old the person is, we entered 45. However, the
number is interpreted as a string, but the inclusion of the int() function helps to convert the number
string to a numerical value. With this, the interpreter then runs a conditional test to compare the age
to see if it is greater than or equal to 40. In this case, our age “45” returns true because it is greater
than 40.
How then can we use the int() function in a real program? Well, let us use a program that checks if
people are tall enough to participate in a basketball match.
basketball_height = input(“What is your current height? It should be in feet’s:”)
basketball_height = int(basketball_height)
if basketball_height >= 7:
print( “\nYou have what it takes to participate in a basketball match!”)
else:
print(“\nYou do not meet up with the requirement for this match!”)
This program compares the height of the person to 7 inches because we already convert as a
numerical value. If the user inserts anything below 7, the message “You do not meet up with the
requirement for this match!” will display. However, let us assume the user input 8, the output will be
as follow:
What is your current height? It should be in feet’s: 8
You have what it takes to participate in a basketball match!
Do'stlaringiz bilan baham: