모두를 위한 파이썬_Chapter05

제이브로·2021년 10월 7일
0

모두를위한파이썬

목록 보기
6/13

Chapter 05

5.2 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.

largest = None
smallest = None

while True:
    num = input("Enter a number: ")
    if num == "done":
        break
        
    try:
        num1 = int(num)
    except:
        print("Invalid input")
        continue
    if largest is None or num1 > largest:
        largest = num1
    elif smallest is None or num1 < smallest:
        smallest = num1

print("Maximum is", largest)
print("Minimum is", smallest)
profile
기록하지 않으면 기록되지 않는다.

0개의 댓글