Home > Enterprise >  Can someone help me figure out why it keeps saying that largest is undefined? Smallest runs fine
Can someone help me figure out why it keeps saying that largest is undefined? Smallest runs fine

Time:02-13

firstNumber = -50;
secondNumber = 53;
thirdNumber = 78;

# Write assignment, if, or if else statements here as appropriate
if secondNumber>firstNumber:
    if secondNumber>thirdNumber:
        largest=secondNumber
elif firstNumber>secondNumber:
    if firstNumber>thirdNumber:
        largest=firstNumber
else:
    largest=thirdNumber
if secondNumber<firstNumber:
    if secondNumber<thirdNumber:
        smallest=secondNumber
elif firstNumber<secondNumber:
    if firstNumber<thirdNumber:
        smallest=firstNumber
else:
    smallest=thirdNumber

# Output largest and smallest number. 
print("The largest value is "   str(largest))
print("The smallest value is "   str(smallest))

Can someone help me figure out why it keeps saying that largest is undefined? Smallest runs fine I've been sitting here for the past 30 minutes trying to see if I typed largest wrong lol

CodePudding user response:

largest is undefined because there's no condition where largest is created. see the comments I added for additional insight.

firstNumber = -50
secondNumber = 53
thirdNumber = 78

# Write assignment, if, or if else statements here as appropriate
if secondNumber>firstNumber: # is True
    if secondNumber>thirdNumber: # is False
        largest=secondNumber
elif firstNumber>secondNumber: # Skipped because first if is True
    if firstNumber>thirdNumber:
        largest=firstNumber
else: # Skipped because first if is True
    largest=thirdNumber
if secondNumber<firstNumber:
    if secondNumber<thirdNumber:
        smallest=secondNumber
elif firstNumber<secondNumber:
    if firstNumber<thirdNumber:
        smallest=firstNumber
else:
    smallest=thirdNumber

# Output largest and smallest number. 
print("The largest value is "   str(largest))
print("The smallest value is "   str(smallest))

if you remove the nesting, it will work

# Write assignment, if, or if else statements here as appropriate
if secondNumber>firstNumber and secondNumber>thirdNumber:
        largest=secondNumber
elif firstNumber>secondNumber and firstNumber>thirdNumber:
        largest=firstNumber
else: 
    largest=thirdNumber
if secondNumber<firstNumber and secondNumber<thirdNumber:
        smallest=secondNumber
elif firstNumber<secondNumber and firstNumber<thirdNumber:
        smallest=firstNumber
else:
    smallest=thirdNumber

# Output largest and smallest number. 
print("The largest value is "   str(largest))
print("The smallest value is "   str(smallest))

CodePudding user response:

The problem is subtle. What is happening here is, if one of the top-level if statments if true, then the elif and else statments is automatically being not considered. So in this example, because your second number is larger than your first, BUT NOT larger than the second, the code goes down the path of your first if statment, but since the second if statment within is not true, the variable "largest" actually never gets assigned.

An alternative way is to use a "and" syntax on your if statments e.g.,

if secondNumber>firstNumber and secondNumber>thirdNumber: 
    largest = secondNumber
  • Related