Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not getting correct output
#1
Hi,
Am new to python programming, one of the problems am working is resulting in incorrect results, can someone please help me on it.
Appreciate your help!!!

Q: 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.
Input Example is 7, 2, bob, 10, and 4

Below is my code:
largest = None
smallest = None
while True:
    try:
        num = input("Enter a number: ")
        if num == "done" :
            break
            print(num)
        for i_num in num :
            if i_num > largest :
                largest = i_num
                print('largest',i_num)
            else :
                for i_num in num :
                    if smallest is None:
                        smallest = i_num
                    elif i_num < smallest:
                        smallest=i_num
                print('smallest',i_num)
        print("Maximum", largest)
        print("Minimum",smallest)
    except:
        if i_num == 'bob':
            print("Invalid Input",i_num)
        #continue
print("Maximum", largest)
print("Minimum",smallest)
Reply
#2
Twice you do for i_num in num. The input function returns a string, so num is a string. Looping over a string give you the characters of the string, not numbers you can compare to largest and smallest. You need to convert num to a number using int(), and then compare. And the try/except should just be around that conversion, and the except should be except ValueError:, to catch only conversion errors. As it is, your try/except is catching any error from almost all of your code, which is probably disguising errors.

In the future, please give the full traceback of any error message, or a clear description of how the output is not correct.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for response!
After converting to integer, am not getting error message but my results are no correct. please help me on it.
largest = None
smallest = None
while True:
    try:
        num = input("Enter a number: ")
        if num == "done" :
            break
            print(num)
        num_list = int(num)
        for i_num in num_list :
            if i_num > largest :
                largest = i_num
                print('largest',i_num)
            else :
                for i_num in num_list :
                    if smallest is None:
                        smallest = i_num
                    elif i_num < smallest:
                        smallest=i_num
                        print('smallest',i_num)
                        print("Maximum", largest)
                        print("Minimum",smallest)
    except:
            if num == 'bob':
                print("invalid input")
            continue
        #continue
print("Maximum", largest)
print("Minimum",smallest)
Results:
Output:
Enter a number: 7 Enter a number: 2 Enter a number: bob invalid input Enter a number: 10 Enter a number: 4 Enter a number: done Maximum None Minimum None

This is the changed code I have now
while True:
    largest = None
    smallest = None
    try:
        num = input("Enter a number: ")
        if num == "done" :
            break
            print(num)
        else:
            num_list = int(num)
            for i_num in num_list :
                if i_num > largest :
                    largest = i_num
                    print('largest',i_num)
                else :
                    for i_num in num_list :
                        if smallest is None:
                            smallest = i_num
                        elif i_num < smallest:
                            smallest=i_num
                            print('smallest',i_num)
            print("Maximum", largest)
            print("Minimum",smallest)
    except:
            if num == 'bob':
                print("invalid input")
            continue
        #continue
print("Maximum", largest)
print("Minimum",smallest)
Output:
Output Results : Enter a number: 5 Enter a number: 90 Enter a number: 34 Enter a number: bob invalid input Enter a number: done Maximum None Minimum None
Reply
#4
And this is exactly what I was talking about with the try/except. Line 11 is throwing an error when you try to iterate over a number, and it is being caught by your plain except statement.

You don't need any for loops in this code. As each number comes in, just check it against smallest and largest, and update them as necessary.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
How does the iteration happen to find largest & smallest number in given list if there's no FOR loop?

while True:
    largest=None
    smallest=None
    try:
        num = input("Enter a number: ")
        if num == "done" :
            break
            print(num)
        else:
            num_list = int(num)
            #for i_num in num_list :
            if i_num > largest :
                largest = i_num
                print('largest',i_num)
            else :
                    #for i_num in num_list :
                if smallest is None:
                    smallest = i_num
                elif i_num < smallest:
                    smallest=i_num
                    print('smallest',i_num)
        print("Maximum", largest)
        print("Minimum",smallest)
    except:
        if num == 'bob':
            print("invalid input")
        continue
print("Maximum", largest)
print("Minimum",smallest)
Results:
Enter a number: 7
Enter a number: 2
Enter a number: bob
invalid input
Enter a number: 10
Enter a number: 4
Enter a number: done
Maximum None
Minimum None
Reply
#6
Fix your try/except. It is still blocking errors, and until you fix it, you are not going to see the errors in your code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Four sequential bytes; Need to remove high order bit in each to get correct output GrandSean 5 2,885 Feb-06-2021, 07:17 PM
Last Post: GrandSean
  attribute error instead of correct output MaartenRo 2 2,147 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  Not quite getting the correct Output from a function twinpiques 3 2,622 Aug-04-2019, 11:53 PM
Last Post: twinpiques

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020