How to find the second lowest digit in int number with Python

2 Answers

0 votes
def getTheSecondLowestDigit(n):
    min = 9
    befor_min = min
    while (n):
        x = n % 10
        if x < min: 
            befor_min = min
            min = x
        elif (x < befor_min and x != min):
            befor_min = x  
        n = int(n / 10)
    return befor_min
      
      
n = 213
print(getTheSecondLowestDigit(n))
        
n = 76594
print(getTheSecondLowestDigit(n))
        
n = 76429
print(getTheSecondLowestDigit(n))
      
n = 76300
print(getTheSecondLowestDigit(n))
      
n = 111
if (getTheSecondLowestDigit(n) == 9):
    print("There is no second lowest number")
  
  
  
  
'''
run:
  
2
5
4
3
There is no second lowest number
 
'''

 



answered Jun 9, 2020 by avibootz
edited Jun 9, 2020 by avibootz
0 votes
n = 213
print(sorted(list(str(n)), reverse=True)[-2])
        
n = 76594
print(sorted(list(str(n)), reverse=True)[-2])
        
n = 76429
print(sorted(list(str(n)), reverse=True)[-2])

  
  
'''
run:
  
2
5
4
 
'''

 



answered Jun 9, 2020 by avibootz

Related questions

1 answer 215 views
1 answer 163 views
1 answer 142 views
1 answer 159 views
1 answer 139 views
1 answer 168 views
...