-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecond_smallest.py
42 lines (40 loc) · 1.11 KB
/
second_smallest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'''
Program: WAP to input 3 numbers and find the second smallest.
Author: Bikramadittya Bagchi
Date: 02-02-2021
'''
import re
# Declarations
regex_float = '[+-]?[0-9]+\.[0-9]+'
regex_int = "[-+]?[0-9]+$"
small_list = []
small_1 = 0
small_2 = 0
# Taking input from users
print("Enter 3 numbers for finding smallest among them")
for i in range(3):
inp = input("Enter any number to insert: ")
if re.search(regex_float, inp):
inp = float(inp)
small_list.append(inp)
elif re.search(regex_int, inp):
inp = int(inp)
small_list.append(inp)
else:
print("Invalid data entered!")
break
# Processing data
if len(small_list) == 3:
for item in small_list[1:]:
if item < small_1:
small_2 = small_1
small_1 = item
elif small_2 == 0 or small_2 > item:
small_2 = item
# Display output
print("The entered elements are: ", end=" ")
for item in small_list:
print(item, end=" ")
print(f"and, the second smallest number is: {small_2}")
else:
print("Unable to compute data! Insufficient data received!")