How to check if a number is 2 digit in python
I have created a function which checks if the number has any other digit other than 3 or 5, return false. Show
But unfortunately it's not working! Can I know the problem? asked Mar 7, 2014 at 13:40
5 It's always true that Change the Alternatively, check it as
In fact, we don't need to convert the characters to ints just to check equality, and you could just do:
Or even Another option is to use sets:
answered Mar 7, 2014 at 13:43
RemcoGerlichRemcoGerlich 29.6k5 gold badges63 silver badges78 bronze badges 5 Since the why part has been already answered, let me show you how to do this efficiently
The sister version of the same
answered Mar 7, 2014 at 13:48
thefourtheyethefourtheye 225k52 gold badges441 silver badges485 bronze badges 1 Your issue is that you are using an Just change your code to this :
And then everything works ! answered Mar 7, 2014 at 13:43
I suggest avoiding the conversion to a string:
answered Mar 7, 2014 at 13:48
tmr232tmr232 1,16115 silver badges22 bronze badges 2 You can use set:
answered Mar 7, 2014 at 13:50
venpavenpa 4,14819 silver badges23 bronze badges 4 Other's have shown the problem in your code, here's an alternative.
It returns answered Mar 7, 2014 at 13:48
GuyGuy 6055 silver badges21 bronze badges Another solution:
Output:
answered Mar 7, 2014 at 13:50
James SapamJames Sapam 15.2k11 gold badges47 silver badges68 bronze badges 5 How do you check if a number is a single digit in Python?You can check if a string, x , is a single digit natural number by checking if the string contains digits and the integer equivalent of those digits is between 1 and 9, i.e. Also, if x.
How do you count the number of digits in Python?Python Program to Count the Number of Digits in a Number. Take the value of the integer and store in a variable.. Using a while loop, get each digit of the number and increment the count each time a digit is obtained.. Print the number of digits in the given integer.. How do you check a number is 3 digit number or not?Program Explanation
check whether the num is greater than 99 and less than 100 using if statement. if it is true, then print num is three digit number using printf statement. Else print num is not three digit number using printf statement.
How do you find the 3 digit number in Python?You can use regex to test it's a three digit number. For example: \d{3} or [0-9]{3}. If the value fails the test, have the function call itself (recursion) and re-prompt the user for the number.
|