Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help
#1
# 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)
Reply
#2
What output you get and what is expected, i.e. what is "good output"
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
Hint: should you be using num for the comparisons? What type is it?
Reply
#5
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)
Reply
#6
You got it to work because of using fval instead of num, not because if using int instead of float
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020