Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework 5.2
#1
Hello everyone,

I was trying to write a code which would repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers.

However, the output has both Largest and Smallest staying at "None", regardless of the inputs. Please advise. Thank you!!

largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
        break

    try:
        number = float(num)
    except:
        print("Invalid input")
    continue

    if largest is None:
        largest = number
    if number > largest:
        largest = number

    if smallest is None:
        smallest = number
    if number > smallest:
        smallest = number


def done(largest,smallest):
    print("Maximum", largest)
    print("Minimum", smallest)

done(largest, smallest)
Reply
#2
It looks like you need to indent "continue".
Reply
#3
(Oct-15-2019, 08:23 PM)micseydel Wrote: It looks like you need to indent "continue".

thanks! However, if I input the following numbers: 2, 5, 10, 7. The output for both Largest and Smallest are 10.
Reply
#4
check the comparison on line 21
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
#5
(Oct-15-2019, 08:29 PM)speedy1668 Wrote:
(Oct-15-2019, 08:23 PM)micseydel Wrote: It looks like you need to indent "continue".

thanks! However, if I input the following numbers: 2, 5, 10, 7. The output for both Largest and Smallest are 10.

silly me... Thanks a lot!
Reply


Forum Jump:

User Panel Messages

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