0

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)
J.Adhikari
  • 125
  • 1
  • 13
  • 2
    Is your question: *"How to display the largest and smallest number in python"* or is it: *"How can I validate user input can only take numbers."*? – Dimitris Fasarakis Hilliard Sep 25 '16 at 13:09
  • 1
    Read about exception handling. You want to wrap your `int` casting with a `try/except` so that you can handle the cases when you pass a non-number. Also, @JimFasarakis-Hilliard brings up a good point, your question title is asking one thing, and in your question you are asking another. Please make sure relevancy and consistency are kept between your title and what you are asking in your question. – idjaw Sep 25 '16 at 13:10

2 Answers2

1

Here's how I might modify your code above. This accomplishes two things, make sure that the input is in digit form, and cleans up finding the largest/smallest numbers in the array. Granted, it will infinitely loop if the user doesn't enter a digit but you can always add a max tries to each loop (I leave that as an exercise for the reader).

Edit: Corrected a bug...

input_set = list()
num = None
while num is None:
    ret = input("Enter how many elements you want:")
    try:
        num = int(ret)
    except ValueError:
        print ("Invalid input, please enter a digit.")

print("Enter numbers in array:")
for i in range(num):
    n = None
    while n is None:
        ret = input("input number :")
        try:
            n = int(ret)
            input_set.append(n)
        except ValueError:
            print ("Invalid input, please enter a digit.")

print ("ARRAY: ", input_set)
print ("Largest number is :", max(input_set))
print ("Smallest number is :", min(input_set))
Matt W
  • 126
  • 1
  • 7
1

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)
XZ6H
  • 1,779
  • 19
  • 25