-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is working fine with clear logic