-1

Coursera python assignment

I do not inderstand what is wrong with this code. It works well for the largest, but not the smallest. And both line of codes are the same! I am new here and new to programming as well, pardon my inexperence . Please note that I need to solve this problem with these methodes as it was the chapter in the course. Sorry again

largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if (num=="done"): 
        break
    else:
        try:
            num=int(num)
        except:
            print("Invalid input") 
            continue
    if(num < smallest):
        smallest = num
    elif(num > largest):
        largest = num
print("Maximum is", largest)
print("Minimum is", smallest)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Please put a [mre], **as text**, in the question. – jonrsharpe Jul 01 '20 at 18:12
  • You can't compare a number with `None`. So `if (num < smallest)` won't work because you set `smallest` to `None`. – Barmar Jul 01 '20 at 18:13
  • 2
    "see the shot" no, please provide **all code as formatted text in the question itself**. See [this](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) for more information on formatting code blocks – juanpa.arrivillaga Jul 01 '20 at 18:13
  • If you only enter one number, it should be both largest and smallest. But your code will only assign one or the other. – Barmar Jul 01 '20 at 18:15

3 Answers3

0

You shouldn't set smallest and largest to None, because you can't compare this with a number.

Instead, you should set them both to the first input. Then you can compare additional numbers to them. And if only one number is entered, it will be the mamimum and minimum.

I've moved reading the number into a function, to avoid repeating the validation loop in both places.

def get_number():
    while True:
        num = input("Enter a number: ")
        if num == "done":
            return num
        try:
            num = int(num)
            return num
        except:
            print("Invalid input")

smallest = get_number()
largest = smallest
if smallest == "done":
    print("At least one number must be entered")
else:
    while True:
        num = get_number()
        if num == "done":
            break
        if num < smallest:
            smallest = num
        elif num > largest:
            largest = num
    print("Maximum = ", largest)
    print("Minimum = ", smallest)

Test results:

$ python testminmax.py
Enter a number: 7
Enter a number: 2
Enter a number: bob
Invalid input
Enter a number: 10
Enter a number: 4
Enter a number: done
Maximum =  10
Minimum =  2
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

This problem mostly requires a do-while loop, but as python don't have a do-while loop, you can counter it as follows :

largest = None
smallest = None

first_time = True
while True:
    num = input("Enter a number:")

    if num=="done" :
        break
    else:
        try:
            num = int(num)
        except:
            print("Invalid input")
            continue
        if first_time :
            largest = smallest = num
            first_time = False
        else:
            if num < smallest :
                smallest = num
            elif num > largest :
                largest = num

print("Max : ", largest)
print("Min : ", smallest)

The first time a number is entered, it is checked if it's a number and then assigned to smallest and largest both. After first time, user can enter number, and it works fine. Hope it helps :)

faiz-e
  • 190
  • 4
  • 21
0

GOT IT! I should have used ** is ** istead of ** < **!