It give error "bad input on line 13."
Problem statement: "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:
num=int(num)
except:
print("Invalid input")
continue
if smallest is None:
smallest=num
elif num < smallest:
smallest=num
if largest is None:
largest=num
elif num > largest:
largest=num
print("Maximum is",largest)
print("Minimum is",smallest)
On line 7, you don't have num=int(num) indented for the try statement. When I indented that, it ran for me.
Related
[My solution screenshot][1]
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:
no=int(num)
except:
print("Invalid input")
if largest is None or no > largest:
largest = no
if smallest is None or no < smallest:
smallest = no
print("Maximum is", largest)
print("Minimum is", smallest)
My output:
Invalid input
Maximum is 10
Minimum is 2
← Mismatch
Desired Output:
Invalid input
Maximum is 10
Minimum is 2
As you can see my output is same but it still gives me error.
Help me anyone.
[1]: https://i.stack.imgur.com/D6wCE.png
In Python, break statement doesn't require semicolon. Remove semicolon from break statement
Modify your code in below mentioned way, so that no need to check largest and smallest value for invalid input.
largest = None
smallest = None
while True:
try:
num = int(input("Enter the Integer"))
if num == "done":
break
if largest is None or num > largest:
largest = num
if smallest is None or num < smallest:
smallest = num
except:
print("Invalid Input")
print("Maximum is ", largest)
print("Minimum is ", smallest)
can you please tell me why do i get error for this?
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:
try:
num=input("Enter a number: ")
if num == 'done':
break
num=int(num)
if largest == None or largest<num:
largest = num
elif smallest == None or smallest > num:
smallest = num
except ValueError:
print ("Invalid input")
break
print ("Maximum number is", int(largest))
print ("Minimum number is", int(smallest))
You may be using Python 2 instead of Python 3 - please check this. If you are using Python 2, you should be using raw_input() instead of input().
Change elif to if:
if largest == None or largest<num:
largest = num
if smallest == None or smallest > num:
smallest = num
That will guarantee both largest and smallest are both integers (unless the first input is done or another invalid value).
You need to check if largest or smallest is None.
Append the user input to a list inside the while loop, and then slice it in the final print statements:
largest = None
smallest = None
nums = []
while True:
try:
num = input("Enter a number: ")
if num == 'done':
break
num = int(num)
nums.append(num)
except ValueError:
print("Invalid input")
break
print("Maximum number is", max(nums))
print("Minimum number is", min(nums))
Example run:
Enter a number: 22
Enter a number: 2
Enter a number: done
Maximum number is 22
Minimum number is 2
Posting the output and/or the error would be very helpful to help you.
You have to change elif into if and remove the break clause inside the except in order to keep asking for numbers even after an invalid input.
largest = None
smallest = None
while True:
try:
num = input("Enter a number: ")
if num == 'done':
break
num=int(num)
if largest == None or largest < num:
largest = num
if smallest == None or smallest > num:
smallest = num
except ValueError:
print ("Invalid input")
print ("Maximum number is", int(largest))
print ("Minimum number is", int(smallest))
question:
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 it.
Input:
7 ,2 , bob, 10, 4, done.
Desired output:
Invalid input
Maximum is 10
Minimum is 2
Actual output:
Invalid input
Invalid input
Maximum is 10
Minimum is 2
Code:
largest=-1
smallest=None
while True:
num =input("Enter a number: ")
try:
if num == "done" :
break
elif smallest is None:
smallest=int(num)
elif int(num)<smallest:
smallest=int(num)
elif int(num)>largest:
largest=int(num)
else:
raise ValueError()
except ValueError:
print("Invalid input")
print("Maximum is",largest)
print("Minimum is",smallest)
I think there's a more Pythonic way of doing this. Try this:
inputList = []
while True:
num = input("Enter a number:")
try:
num = int(num)
inputList.append(num)
except:
if num == "done":
break
else:
print ("Invalid input. Ignoring...")
print ("Maximum is:",max(inputList))
print ("Minimum is:",min(inputList))
Edit: This code works with Python3. For Python2, you might want to use raw_input() instead of input()
You are already capturing the ValueError in Exception,
So inside, try, you are raising ValueError there you leave the scope for this error.
When you accept input, and it accepts 4 as input, which is neither larger than largest (i.e. 10) nor smaller than the smallest (i.e. 2). So it lands in else part and raises ValueError (as per your code). Hence prints Invalid input twice in your case. So this part is unnecessary as well as makes your solution bogus.
Again, from efficiency point of view -
1 - You are checking smallest == None for every input, which takes O(1) time and is unnecessary if you take it any integer
Here is the solution you are looking for :-
largest=None
smallest=None
while True:
try:
num = input("Enter a number: ")
num = int(num)
if smallest is None:
smallest = num
if largest is None:
largest = num
if num < smallest:
smallest = num
elif num > largest:
largest = num
except ValueError:
if num == 'done':
break
else:
print("Invalid input")
continue
print("Maximum is",largest)
print("Minimum is",smallest)
This is the exercise.
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 the numbers from the
book for problem 5.1 and Match the desired output as shown.
The result should be:
Invalid input
Maximum is 7
Minimum is 4
My code:
largest = None
smallest = None
while True:
num = raw_input("Enter a number: ")
if num == "done" : break
if len(num) < 1 : break
try :
num = int(num)
except :
print "Invalid input"
continue
print "Maximum", largest
print "Minimum", smallest
Why is the program not printing out the largest and smallest?
What am I doing wrong?
You never entered a value for largest and smallest.
largest = float('-inf') # Always smaller than any number
smallest = float('inf') # Always larger than any number
while True:
num = raw_input("Enter a number: ")
if num == "done" : break
if len(num) < 1 : break
try :
num = int(num)
except :
print "Invalid input"
continue
# set largest and smallest
# initial inf forces first entry to reset the value
largest = max(largest, num)
smallest = min(smallest, num)
# Because None is always smaller than any integer
print "Maximum", largest
print "Minimum", smallest
I'm trying to create an program in python for a class and I can't figure out why I can't get the expected output.
The program is to take user input values until a 'done' is entered, and then print the max and min of these values. It also has some error checking for valid input (number and not text).
largest = None
smallest = None
while True:
inp = raw_input("Enter a number: ")
if inp == "done" :
break
try:
num=float(inp)
except:
print "Invalid input"
continue
if inp>largest:
largest=inp
if inp<smallest:
smallest=inp
print "Maximum is ", largest
print "Minimum is ", smallest
The loop breaks properly if 'done' is inserted. It doesn't fail if a text string is entered, but also doesn't print "Invalid input". I'm not asking for someone to solve my homework program, but to provide me with an explanation as to why I never get largest or smallest to be anything other than their original assignment of "None".
Thanks in advance.
largest = None
smallest = None
num = None
while True:
inp = raw_input("Enter a number: ")
if inp == "done" :
break
try:
num=float(inp)
except:
print ("Invalid input")
continue
if largest is None:
largest = num
smallest = num
if num>largest:
largest=num
if num<smallest:
smallest=num
print ("Maximum is ", largest)
print ("Minimum is ", smallest)
if smallest is None or smallest< inp:
smallest=inp
if largest is None or largest > inp:
largest=inp
You need to do something with your comparison operators bro. Right now you're checking if smallest is smaller than input (and vice versa for largest). The result: you're switching max and min. Should've been like this:
if smallest is None or inp < smallest:
smallest = inp
if largest is None or inp > largest:
largest = inp
It doesn't report the proper max or min because you use inp (the string) in your comparisons instead of num (the float).
This is the final code I used. With help from all of you that responded! Thanks to all for their time.
largest = None
smallest = None
while True:
inp = raw_input("Enter a number: ")
if inp == "done" :
break
try:
num=float(inp)
except:
print ("Invalid input")
continue
if smallest is None or smallest< inp:
smallest=inp
if largest is None or largest > inp:
largest=inp
print ("Maximum is ", largest)
print ("Minimum is ", smallest)
Don't use None as your initialization, use floats.
largest =float("-inf") # negative infinity
smallest = float("inf") # infinity
while True:
inp = raw_input("Enter a number: ")
if inp == "done" :
break
try:
num = float(inp)
largest = max(largest, num)
smallest = min(smallest, num)
except:
print "Invalid input"
continue
print "Maximum is ", largest
print "Minimum is ", smallest
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
num = int(num)
except:
print("Invalid input")
continue
if smallest == None or num < smallest:
smallest = num
if largest == None or num > largest:
largest = num
print("Maximum is", largest)
print("Minimum is", smallest)
It works, kinda
python27 test.py
Enter a number: 10
Enter a number: 20
Enter a number: 40
Enter a number: done
Maximum is 40
Minimum is None
You will never get a min because a float is never less than None.