Skip to content

Instantly share code, notes, and snippets.

@abdoulayegk
Forked from AmruthPillai/Program52.py
Last active June 18, 2020 08:30
Show Gist options
  • Save abdoulayegk/4d5a55249a81c69628ad4b54016037a4 to your computer and use it in GitHub Desktop.
Save abdoulayegk/4d5a55249a81c69628ad4b54016037a4 to your computer and use it in GitHub Desktop.
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number.
"""
Working code
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number.
"""
l = []
while True:
num = input("Enter a number:")
if num == 'done':
break
try:
x = int(num)
except ValueError:
print('Non-numeric data found in the file.')
continue
l.append(x)
for i in range(len(l)):
for j in range(len(l)):
if l[i]< l[j]:
# sorting the list in assending order
l[i],l[j] = l[j],l[i]
# l[0] is the smallest element of the list if the list is well sorted
minimum = l[0]
# l[-1] is the largest element which is at the last position when it is well sorted
maximum = l[-1]
print("Maximum is :", maximum)
print("Minimum is :", minimum)
@abdoulayegk
Copy link
Author

This code is working fine with clear logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment