Iniciar sesiónCrear cuenta

# Python - Imprimir numero maximo y minimo en un infinite loop

Vista:

## 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:

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
Val: 819

## Imprimir numero maximo y minimo en un infinite loop

Publicado por algoritmo (234 intervenciones) el 04/07/2020 09:26:19
Hola

Espero que te sirva :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
`largest = None`
`smallest = None`
`while True:`
`    number=input("Ingrese un numero")`
`    try:`
`        if number == "done":`
`            print("Done!")`
`            break`
`        elif number.isalpha():`
`            print('No es un número')`
`        elif largest is None:`
`            y=float(number)`
`            largest=y`
`        elif smallest is None:`
`            y=float(number)`
`            smallest=y`
`        elif smallest<y:`
`            y=float(number)`
`            smallest=y`
`        elif largest>y:`
`            y=float(number)`
`            largest=y`
` `
`    except:`
`        print("Invalid input")`
`        continue`
` `
`print('Mayor: %s' % largest)`
`print('Menor: %s' % smallest)`
Valora esta respuesta
0