Imprimir numero maximo y minimo en un infinite loop
Publicado por Jose Gonzalez (2 intervenciones) el 04/07/2020 05:59:07
Necesito resolver este problema:
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. Enter 7, 2, bob, 10, and 4 and match the output below.
Intente con este codigo pero no me arroja bien los maximos y minimos ni los errores cuando escribo palabras en lugar de números:
Muchas gracias de antemano
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. Enter 7, 2, bob, 10, and 4 and match the output below.
Intente con este codigo pero no me arroja bien los maximos y minimos ni los errores cuando escribo palabras en lugar de números:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
largest = None
smallest = None
while True:
number=input("Ingrese un numero")
try:
if largest is None:
y=float(number)
largest=y
elif smallest is None:
y=float(number)
smallest=y
elif number == "done":
print("Done!")
break
elif smallest<y:
smallest=y
elif largest>y:
largest=y
except:
print("Invalid input")
continue
Muchas gracias de antemano
Valora esta pregunta
0