-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPython Loops Exercis.py
32 lines (29 loc) · 1.06 KB
/
Python Loops Exercis.py
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
30
31
32
# Programming for Everybody (Getting Started with Python)
# Exercise: 5.1(Chapter five)
# 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 :
value = input("Enter a number: ")
if value == "done" : # if user enter done break the loop
break
try : # exception hundlling
value = int(value)
except :
print("Invalid input")
continue # go back and continue
if largest == None :
largest = value
if smallest == None :
smallest = value
elif value < smallest :
smallest = value # Minimum so far
elif value > largest :
largest = value # Maximum so far
# print the result
print("Maximum is", largest)
print("Minimum is", smallest)