J.Adhikari - 1 year ago 210
Python Question

# How to display the largest and smallest number in python

I have two questions:

1. How to display the largest and smallest number which accept 5 numbers from user input

2. How can I validate user input to take only numbers.

Here is my code :

``````input_set = list()
num = input("Enter how many elements you want:")
print("Enter numbers in array:")
for i in range(int(num)):
n = int(input("input number :"))
input_set.append(int(n))
print ("ARRAY: ", input_set)
largest = input_set[0]
for i in range(len(input_set)):

if input_set[i] > largest:
greatest = input_set[i]

print("Largest number is :", greatest)

smallest = input_set[0]
for i in range(len(input_set)):

if input_set[i] < largest:
smallest = input_set[i]

print("Smallest number is :", smallest)
``````

This one takes input till the user enters valid input

``````input_set = list()
num = input("Enter how many elements you want:")
print("Enter numbers in array:")
for i in range(int(num)):
while True:
try:
n = int(input())
except ValueError:
print("Enter a number")
continue
else:
break
input_set.append(int(n))
print ("ARRAY: ", input_set)
largest = input_set[0]
for i in range(len(input_set)):

if input_set[i] > largest:
print('greatest '+str(largest))
print('current '+str(input_set[i]))
largest = input_set[i]

print("Largest number is :", largest)

smallest = input_set[0]
for i in range(len(input_set)):

if input_set[i] < largest:
smallest = input_set[i]

print("Smallest number is :", smallest)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download