![]() |
| Need help - 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: Need help (/thread-29361.html) |
Need help - Chico_Bean008 - Aug-30-2020 # I am not getting a good output through this what could i be doing wrong # also i'm fairly new to this and don't understand why the indents and tags aren't showing up sorry in advance largest = None smallest = None while True: num = input("Enter a number: ") if num == "done" : break try: fval = float(num) except: print('Invalid input') continue if largest is None : largest = num elif num > largest : largest = num if smallest is None : smallest = num elif num < smallest : smallest = num print("Maximum is", largest) print("Minimum is", smallest) RE: Need help - buran - Aug-30-2020 What output you get and what is expected, i.e. what is "good output" RE: Need help - Chico_Bean008 - Aug-30-2020 to get a Maximum of 10 and Minimum of 2 for some reason i keep getting a minimum of 7 when i type the integers 7, 2, bob, 10, and 4 RE: Need help - ndc85430 - Aug-30-2020 Hint: should you be using num for the comparisons? What type is it? RE: Need help - Chico_Bean008 - Aug-30-2020 I got it to work after changing float() to int() and doing. Thanks for the input all! much appreciated! largest = None smallest = None while True: num = input("Enter a number: ") if num == "done" : break try: fval = int(num) except: print('Invalid input') continue if largest is None : largest = fval elif fval > largest : largest = fval if smallest is None : smallest = fval elif fval < smallest : smallest = fval print("Maximum is", largest) print("Minimum is", smallest) RE: Need help - buran - Aug-30-2020 You got it to work because of using fval instead of num, not because if using int instead of float RE: Need help - deanhystad - Aug-30-2020 (Aug-30-2020, 04:28 AM)Chico_Bean008 Wrote: to get a Maximum of 10 and Minimum of 2Your old code prints that 7 is the maximum and 10 the minimum, which is correct for inputs '7', '2', 'bob', '10', and '4'. Your old code sorted the input strings, not the numerical values of the input strings. When I took out the value conversion it chose 'bob' as the maximum value. Python has sorting functions in the standard libraries. |