Python Forum

Full Version: Need help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
# 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)
What output you get and what is expected, i.e. what is "good output"
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
Hint: should you be using num for the comparisons? What type is it?
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)
You got it to work because of using fval instead of num, not because if using int instead of float
(Aug-30-2020, 04:28 AM)Chico_Bean008 Wrote: [ -> ]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
Your 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.