Programming for everybody (Python) Assignment 5.2
Coursera: Programming for everybody with Python
Assignment 5.2
Week 7
Question:-
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.
Solution:-
largest = None
smallest = None
while True:
try:
num = input("Enter a number: ")
if num == "done":
break
#print (num)
num = int(num)
if largest is None or largest < num:
largest = num
smallest = num
except ValueError:
print("Invalid input")
print ("Maximum is", largest)
print ("Minimum is", smallest)
Official website for Coursera:
https://www.coursera.org/learn/python
Assignment 5.2
Week 7
Question:-
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.
Solution:-
largest = None
smallest = None
while True:
try:
num = input("Enter a number: ")
if num == "done":
break
#print (num)
num = int(num)
if largest is None or largest < num:
largest = num
- elif smallest is None or smallest > num:
smallest = num
except ValueError:
print("Invalid input")
print ("Maximum is", largest)
print ("Minimum is", smallest)
Official website for Coursera:
https://www.coursera.org/learn/python
Comments
Post a Comment
Comments here for more information.........