largest = None smallest = None while True: num = input("Enter a number: ") if num == "done": break try: fnum = float(num) except: print("Invalid input") continue if largest is None : largest = float(num) elif num > largest : largest = float(num) if smallest is None : smallest = float(num) elif num < smallest : smallest = float(num) print(num) print("Min:",smallest) print("Max:",largest) print("Maximum is",int(largest)) print("Minimum is",int(smallest))Can someone help point to where I have gone wrong with the logic please? Thanks!
[split] Python for Everybody 5.2 assignment
[split] Python for Everybody 5.2 assignment
|
Jun-14-2019, 09:10 PM
Can you be more specific? What inputs are you using? How exactly is the behavior different from what you want?
Jun-14-2019, 09:45 PM
Oct-21-2019, 12:04 PM
Here the code will help you...……
largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except ValueError: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest)
As far as my googling went I understood that there are basically no limitations regarding solution.
So one can take advantage of Python 3.8 new cool feature and of course use built-in min and max to deliver result: ints = [] while (answer := input('Enter integer: ').lower()) != 'done': try: ints.append(int(answer)) except ValueError: print(f"{answer!r} can't be interpreted as integer") print(f'Maximum entered value was {max(ints)} and minimum value was {min(ints)}')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. |
|
Users browsing this thread: 1 Guest(s)