Code is returning the incorrect values. syntax error - 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: Code is returning the incorrect values. syntax error (/thread-40201.html) |
Code is returning the incorrect values. syntax error - 007sonic - Jun-18-2023 Just trying to figure out why my code here is returning the incorrect minimum and maximum values. tried flipping the ">" and "<" around and it still doesn't work. I'm not meant to use the min() and max() functions here. My thought was maybe I should hold all of the input values in another variable and then check that one for the min and max values? largest = None smallest = None while True: num = input("Enter a number: ") if num == "done": break try: int(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(num) print("Maximum is", largest, "Minimum is", smallest)
RE: Code is returning the incorrect values. syntax error - rob101 - Jun-18-2023 One way to do this, without using min/max, would be to have the values stored in a list object, then do a .sort() , so that that you can use the index instead of the min/max functions: the minimum and maximum values would then be at index zero and index -1
RE: Code is returning the incorrect values. syntax error - 007sonic - Jun-18-2023 (Jun-18-2023, 05:24 PM)rob101 Wrote: One way to do this, without using min/max, would be to have the values stored in a list object, then do a Thanks I figured I would need to store all the values somewhere, but wasn't sure how. still new to working with python. RE: Code is returning the incorrect values. syntax error - rob101 - Jun-18-2023 No worries. Post back if you get stuck, but I'm as sure as I can be, based on your grasp of Python thus far, that you'll work it out in no time flat. You've made a good start, so keep at it. RE: Code is returning the incorrect values. syntax error - bowlofred - Jun-18-2023 On line 4, you input() num. That means you have a string.On line 7 and 8, you test if the string can be interpreted as an integer, but you don't change anything. So num is still a string. So in the rest of the program you're doing string comparisons, not numeric comparisons. RE: Code is returning the incorrect values. syntax error - deanhystad - Jun-18-2023 And your logic is wrong. Once you change the code so "num" is an int, this will give you the wrong answer. elif num < largest : largest = numThis will assign the smallest value entered to largest. You made the same mistake with smallest which will hold the largest value you entered. RE: Code is returning the incorrect values. syntax error - 007sonic - Jun-19-2023 (Jun-18-2023, 07:05 PM)bowlofred Wrote: On line 4, you OMG I did forget to change num to an int Thanks! Edit: Yea it was that one simple mistake I knew I had everything right for the most part I just couldn't figure out what was going on. Also I did know I put the > and < in the wrong spots I was trying to troubleshoot and thought that might have been the problem. Thank you so much everyone! |