I want that when the user types a phone number by keyboard it is saved in this variable (number)
number = input(int("digite un numero teléfono :"))
and then return if those numbers that are stored in the variable have more than 9 characters. I did it this way but I couldn't.
if number.len > 9:
print("introduciste mas de 9 numeros")
First, inside the input you only have to place the message that you want to be shown to the user, what you want to do would be done in this way
number = int(input("Enter a phone number :"))
But that would cause an error if the user enters letters, for now I'll tell you my proposed solution which doesn't include that method.
The user is prompted to enter the phone number.
First, isdigit() evaluates a string and if it is clearly numbers, it returns a boolean value
True
, otherwise it returnsFalse
, if for example we evaluate the following:That will return
False
because the variablefoo
does not contain only numbers, and since there is a combination of numbers and letters, it becomes astring
.Ok, so in the loop
while
, when we prefixnot
the evaluation of the variablenumber
in simple words we are saying:While the evaluation of
number.isdigit()
NOT returnsTrue
will execute what is inside thewhile
, remember that a loopwhile
will execute until one of its conditions (or the only one it has) is false, that isFalse
. So when we enter astring
in our script, itnumber.isdigit()
returnsFalse
, and as we said before, as long asnumber.isdigit()
no (not
) returns weTrue
will execute the contents of the loopwhile
. Now, the second condition is easier, if the length of the entered value is greater than 9, we will continue to execute thewhile
.So, as we say, as long as the loop
while
keeps getting True values, it will keep executing, let's put it like this:If the variable
number
is equal to:With this cycle
while
, 2 birds are killed with one stone, it is evaluated if what is entered is a number and its length, you only have to understand how a while cycle works to understand what is happening.