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 .Write code in python
Answers
Answer:
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.
"""
largest = None
smallest = None
while True:
try:
num = raw_input("Enter a number: ")
if num == 'done':
break;
n = int(num)
largest = num if largest < num or largest == None else largest
smallest = num if smallest > num or smallest == None else smallest
except:
print "Invalid input"
print "Maximum number is ", largest
print "Minimum number is ", smallest
@jaygala223
jaygala223
commented about 2 months ago
i wont get 2 as the minimum in output. Please give it a look
@jaygala223
jaygala223
commented about 2 months ago
im getting 7 as minimum but i also gave 2 as an input
@Shankar49
Shankar49
commented about 1 month ago
largest = None
smallest = None
while True:
try:
num = raw_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)
#Enter 7,2,bob,10,4 u will get desired output
@bvsganesh
bvsganesh
commented about 1 month ago
I am getting error
As
Minimum is none
Maximum is none
Above code shankar49
@Shankar49
Shankar49
commented about 1 month ago
u have to Enter 7,2,bob,10,4 ,done
@danialishere
danialishere
commented about 1 month ago
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" :
break
try:
num = int (num)
if num > largest :
largest = num
if num < smallest :
smallest = num
except:
print("Invalid input")
print("Maximum is", largest)
print("Minimum is", smallest)
i'm not getting 2 as the smallest :'(
@hamzapucit
hamzapucit
commented about 1 month ago
largest = None
smallest = None
while True:
inp = input("Enter a number: ")
if inp == "done":
break
try:
num = int(inp)
except : #and not all errors!
print ("Invalid input")
else:
# This block will execute if no exception is caught.
# Yes, this is valid python.
if smallest is None: #first number!
smallest = num
largest = num
elif num < smallest:
smallest = num
elif num > largest:
largest = num
print ("Maximum is", largest)
print ("Minimum is", smallest)
@Jajabor1294
Jajabor1294
commented about 1 month ago
The input box is asking enter number again and again like in never ending loop!! what is the issue??
Not giving the desired output
@donot-liketoCODE
donot-liketoCODE
commented about 1 month ago
`largest = None
smallest = None
while True:
inp = raw_input("Enter a number: ")
if inp == "done" : break
try:
num = float(inp)
except:
# print "Please enter a number as input or 'done'"
print "Invalid input"
continue
if smallest is None:
smallest = num
if num > largest :
largest = num
elif num < smallest :
smallest = num
def done(largest,smallest):
print "Maximum is", int(largest)
print "Minimum is", int(smallest)
done(largest,smallest)`
@Jajabor1294
Jajabor1294
commented about 1 month ago
2020-05-03
what is the issue?
@maruf4461
maruf4461
commented about 1 month ago
largest=None
smallest=None
while True:
number=input("Enter a Number:")
if number=="done": break
try:
xnumber=int(number)
except:
print("Invalid input")
continue
if largest is None:
largest=xnumber
elif largest<xnumber:
largest=xnumber
if smallest is None:
smallest=xnumber
elif smallest>xnumber:
smallest=xnumber
print("Maximum is", largest)
print("Minimum is", smallest)
@Dharanish-py
Dharanish-py
commented about 1 month ago
Capture
Any idea !
@nitish2608
nitish2608
commented about 1 month ago
#Working Code
largest = None
smallest = None
while True:
inp = input("Enter a number: ")
if inp == "done" : break
try:
num = int(inp)
except:
print("Invalid input")
continue
if largest is None or largest < num:
largest = num
elif smallest is None or smallest > num:
smallest = num
print("Maximum is",largest)
print("Minimum is",smallest)
@Vinayak002
Vinayak002
commented about 1 month ago
line 5 error
@hamzapucit
hamzapucit
commented about 1 month ago
you can end input by type "done" in input box.
@purva99-khot
purva99-khot
commented about 1 month ago
Here the correct code!!
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" :
break
try:
num1=int(num)
if(num1>largest):
largest=num1
elif smallest is None or num1<smallest:
smallest = num1
except:
print('Invalid input')
continue
print("Maximum is", largest)
print("Minimum is",smallest)
@mahfuz-raihan
mahfuz-raihan
commented about 1 month ago
You can try this maybe it won't disappoint (Python 3)
largest = None
smallest = None
while True:
try:
num = input("Enter a number: ")
if num == 'done':
break
n = int(num)
if largest is None or n > largest:
largest = n
elif smallest is None or n < smallest:
smallest = n
except:
print("Invalid input")
continue
print("Maximum is", largest)
print("Minimum is", smallest
Here is a Python program that prompts the user for integer numbers until the user enters 'done'. It then prints out the largest and smallest of the numbers entered:
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
num = int(num)
if largest is None or num > largest:
largest = num
if smallest is None or num < smallest:
smallest = num
except ValueError:
print("Invalid input")
if largest is not None and smallest is not None:
print("Maximum is", largest)
print("Minimum is", smallest)
In this program, we first initialize the variables largest and smallest to None. We then enter an infinite loop that prompts the user to enter a number. If the user enters the string "done", we break out of the loop.
If the user enters a valid integer number, we convert it to an integer using the int() function and then compare it to the current values of largest and smallest. If the number is larger than the current value of largest, we update largest to the new number. If the number is smaller than the current value of smallest, we update smallest to the new number.
If the user enters anything other than a valid integer number, we catch the ValueError exception using a try/except block and print an appropriate message.
Finally, we check if largest and smallest have been updated and print out their values if they have.
To know more:-
https://brainly.in/question/17001455?referrer=searchResults
https://brainly.in/question/20112072?referrer=searchResults
#SPJ6