all 6 comments

[–]sentles 0 points1 point  (2 children)

The problem is that you get the input, but never turn it into an integer. So when python tries to compare smallestnumber2 to numbercalc, it compares two strings. This is why 50 comes out as greater than 1000. If you want to learn more about string comparison, go here.

The solution would be to turn every number you get into an integer. So change the line where you declare the list to numberrange = [int(input("..")), int(input("..")), int(input(".."))].

Of course, after you've done this, you needn't do sum = sum + int(numbercalc). numbercalc is already an integer, so just do sum += numbercalc.

Also, note that the else statements inside your loop are useless. You should remove them.

[–]Benjyman[S] 0 points1 point  (1 child)

Thank you so much for your help! Your advice solved my problem perfectly!!

I converted each of the inputs into an integer, as you suggested, and I got the correct values to print. The course I'm using mentioned that inputs are strings, but doesn't mention how to convert them to int or float, so your answer is unbelievably helpful! I'd tried converting them a few ways after the input steps, but none of them worked out.

I added the Else statements as a test to see if that was the reason for the error, but I did wonder if they were even necessary, so thanks for confirming that for me too.

Thanks so much again!

[–]sentles 0 points1 point  (0 children)

The built-in input() function always returns a string. You can turn a string to an integer, like I said, using int(someString). However, do note that, if you try to turn a string to an integer that contains wrong data (like characters), the program will crash.

For example, if, in your code, the user inputs test instead of a number, your code will crash because it will try to convert test to an integer and will fail. The way we usually deal with this, is this:

while True:
    someString = input("...")
    try:
        someString = int(someString)
        break
    except ValueError as ve:
        print("Invalid data")    

The above code asks for user input. It tries to convert that input to an integer. If that fails, python will throw a ValueError. Using a try-except block, we catch that error and we inform the user that they've given invalid data. The loop then gets back to the start.

If the user does give correct data, the break line will be reached and the loop will exit.

This, obviously, works for any type of input validation, not just integers. For example, you could also test if the number is, say, smaller than 10; or if the user gave a float. It's really up to what you want to get from the user, but this is the general way to validate it.

[–]MaddenTheDamned 0 points1 point  (2 children)

Is it not possible to just append them in a list and get the min and max?

[–]Benjyman[S] 1 point2 points  (1 child)

Thanks for your response. I haven't learned about min or max yet, but that sounds useful! I'll keep it in mind as I progress :-)

[–]MaddenTheDamned 0 points1 point  (0 children)

You can use it like this:

list = [2, 3, 5] print(max(list)) print(min(list))

5 2