Not getting correct output - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Not getting correct output (/thread-21012.html) |
Not getting correct output - supriya0307 - Sep-10-2019 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) RE: Not getting correct output - ichabod801 - Sep-10-2019 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. RE: Not getting correct output - supriya0307 - Sep-10-2019 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:
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)
RE: Not getting correct output - ichabod801 - Sep-10-2019 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. RE: Not getting correct output - supriya0307 - Sep-10-2019 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 RE: Not getting correct output - ichabod801 - Sep-10-2019 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. |