Why is my code change the smallest variable from 2 to 10?

97 Views Asked by At

my code

largest = None
smallest = None
while True:    
    pick = input("Please Enter a number: ")
    
    try:
        #first look for user to click done then test to make sure input 
        #is an int
        if pick == "done":
            break
        x = int(pick)
        #test print statement
        print("try: success")

    except ValueError:
        #test print statements
        print("Invalid Input")
        print("Except: Success")
        print("largest:", largest)
        print("smallest:", smallest)
        continue

    if largest == None:
        largest = pick
        #test print statement
        print("1: success")
    if smallest == None:
        smallest = pick
        #test print statement
        print("2: success")
    if pick > largest:
        largest = pick   
        #test print statement
        print("3: success")
    if pick < smallest:
        smallest = pick
        #test print statement
        print("4: success")

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

print("Maximum is", largest)
print("Minimum is", smallest)

I'm trying to repeatedly ask the user to enter numbers until they type done. While they enter numbers I have several if statements that will run to keep changing the smallest and largest variable. I also have entered a try/ except to only accept numbers.

I entered the numbers 7,2,bob,10, and 4. Everything was fine until I got to 10. When I entered 10 the smallest variable changed from 2 to 10. The variable smallest was at 2 but changed to 10 off of the fourth if statement (if pick < smallest). I know it only hit that statement because that is the only success print statement that printed. Please let me know where my fault in logic is. Thanks in advance!

4

There are 4 best solutions below

1
ShadowRanger On

You created an integer from the value pick refers to, but never used the converted value again, nor reassigned pick, so pick remained a string, not a number. Lexicographically, '10' is less than '2'.

Your code will work as expected if you change:

x = int(pick)

to:

pick = int(pick)

so all the values used after that point are ints, and use int comparison rules.

As a side-note: When comparing to None, use is and is not, rather than == and !=. It's part of the PEP 8 programming recommendations; in this case, and most cases, code will work just fine if you ignore it, but the code is slower, and risks misbehavior with types that claim to be equal to anything, even though nothing should be equal to None.

1
FibiX On

The issue in your code is that you are comparing integers (x) with strings (pick). When you compare the values entered by the user with largest and smallest, you should be comparing the integers (x) rather than the strings (pick)

0
soyapencil On
x = int(pick)

You take the input value, cast it to integer, but don't use it. So, later in your code when you do

if pick < smallest:
    ...

You are checking if "10" < "2". That is True, because:

  • when you compare two strings, < means "find the string that would be sorted lexicographically first"
  • e.g. "A" is before "B"

As 1 comes before 2, "10" is lexicographically before "2".

Use x in your code to fix:

largest = None
smallest = None
while True:    
    pick = input("Please Enter a number: ")
    
    try:
        #first look for user to click done then test to make sure input 
        #is an int
        if pick == "done":
            break
        x = int(pick)
        #test print statement
        print("try: success")

    except ValueError:
        #test print statements
        print("Invalid Input")
        print("Except: Success")
        print("largest:", largest)
        print("smallest:", smallest)
        continue

    if largest == None:
        largest = x
        #test print statement
        print("1: success")
    if smallest == None:
        smallest = x
        #test print statement
        print("2: success")
    if x > largest:
        largest = x 
        #test print statement
        print("3: success")
    if pick < smallest:
        smallest = x
        #test print statement
        print("4: success")

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

print("Maximum is", largest)
print("Minimum is", smallest)
0
VoNWooDSoN On

The issue with your code is that you're casting pick to an int and storing it in x.

Then, latter down in the program you're using the value of pick to do your comparisons.
"10" is less than "7" when comparing strings. Where int("10") > int("7").

use x in your if-statements and your program will work.