简体   繁体   中英

Can I stop an If statement from running if another's statement's requirements are met in Python?


# ask for user input
num1 = int(input('Input the first number to compare: '))
num2 = int(input('Input the second number to compare: '))
num3 = int(input('Input the third number to compare: '))

# identify which numbers are largest, second smallest and smallest
if (num1 >= num2) and (num1 >= num3):
    largest = num1
elif (num2 >= num1) and (not num3 >= num2):
    largest = num2
else:
    largest = num3

if (num1 <= num2) and (num1 <= num3):
    smallest = num1
elif (num2 <= num1) and (num2 <= num3):
    smallest = num2
else:
    smallest = num3

if (num1 <= num2) and (num1 >= num3) or (num1 >= num2) and (num1 <= num3):
    secsmallest = num1
elif (num2 <= num1) and (num2 >= num3) or (num2 >= num1) and (num2 <= num3):
    secsmallest = num2
else:
    secsmallest = num3

#print after collecting input

# 1

# 1 is largest, 3 is smallest
if (num1 == num2) and (num1 == largest) and (num3 == smallest):
    print(' ')
    print("The first number", num1, ', is the same as the second number', num2, ', and the smallest number is',
      smallest,)

# 1 is second smallest, 3 is largest
elif (num1 == num2) and (num1 == secsmallest) and (num3 == largest):
    print(' ')
    print("The first number", num1, ', is the same as the second number', num2, ', and the largest number is',
      largest,)

# 1 is smallest, 3 is second smallest (largest)
elif (num1 == num2) and (num1 == smallest) and (num3 == secsmallest):
    print(' ')
    print("The first number", num1, ', is the same as the second number', num2, ', and the largest number is',
      secsmallest,)

# 2

# 2 is largest, 1 is smallest
elif (num2 == num3) and (num2 == largest) and (num1 == smallest):
    print(' ')
    print("The second number", num2, ', is the same as the third number', num3, ', and the smallest number is',
          smallest,)

# 2 is second smallest, 1 is largest
elif (num2 == num3) and (num2 == secsmallest) and (num1 == largest):
    print(' ')
    print("The second number", num2, ', is the same as the third number', num3, ', and the largest number is',
          largest,)

# 2 is smallest, 1 is second smallest (largest)
elif (num2 == num3) and (num2 == smallest) and (num1 == secsmallest):
    print(' ')
    print("The second number", num2, ', is the same as the third number', num3, ', and the largest number is',
          secsmallest,)

# 3

# 3 is largest, 2 is smallest
elif num3 == num1 and (num3 == largest) and (num2 == smallest):
    print(' ')
    print("The third number", num3, ', is the same as the first number', num1, ', and smallest number is',
          smallest, '')

# 3 is second smallest, 2 is largest
elif num3 == num1 and (num3 == secsmallest) and (num2 == largest):
    print(' ')
    print("The third number", num3, ', is the same as the first number', num1, ', and largest number is',
          largest, '')

# 3 is smallest, 2 is second smallest (largest)
elif num3 == num1 and (num3 == smallest) and (num2 == secsmallest):
    print(' ')
    print("The third number", num3, ', is the same as the first number', num1, ', and largest number is',
          secsmallest,)

# all numbers are the same? do this:
if num1 == num2 and num2 == num3:
    print('All numbers are the same :)')

# no same numbers? do this:
else:
    print(' ')
    print("The largest number is", largest, ', the next smallest number is', secsmallest, ', and smallest number is',
          smallest,)

Here is my code, it basically tells you which of 3 inputted numbers is the largest, second smallest and smallest and if any numbers are the same. Everything else is fine, but here is an output when I put 3 numbers in:

Input the first number to compare: 10
Input the second number to compare: 10
Input the third number to compare: 10
 
The first number 10 , is the same as the second number 10 , and the smallest number is 10
All numbers are the same :)

Process finished with exit code 0

Is there any way that I could get rid of that first line and just keep it to say that all numbers are the same? If so, I would like to know soon, my deadline to coming up :)

Should be easy to just use the "all same numbers if right after user inputs:

# ask for user input
num1 = int(input('Input the first number to compare: '))
num2 = int(input('Input the second number to compare: '))
num3 = int(input('Input the third number to compare: '))

# all numbers are the same? do this:
if num1 == num2 and num2 == num3:
    print('All numbers are the same :)')

else:
    # identify which numbers are largest, second smallest and smallest
    if (num1 >= num2) and (num1 >= num3):
        largest = num1

    #...snipped

    # 3 is smallest, 2 is second smallest (largest)
    elif num3 == num1 and (num3 == smallest) and (num2 == secsmallest):
        print(' ')
        print("The third number", num3, ', is the same as the first number', num1, ', and largest number is', secsmallest,)

    # no same numbers? do this:
    else:
        print(' ')
        print("The largest number is", largest, ', the next smallest number is', secsmallest, ', and smallest number is', smallest,)

On another note, why not create a list for these numbers? That way you can use min and max for the smallest and largest, and just leaves to somehow get the leftover, the middle one.

An elif (else if) statement will only execute if the if/elif statements before it in the same block evaluate to False . By the same block, I mean the run of if-elif....elif. If you then start a new block with another if statement, that will run as normal.

Thus, if you want something to not execute if something else is true, you could put it in an elif or else block after the initial condition.

If you don't want them located next to each other in code, you could also set a flag and refer back to it later.

See this FizzBuzz Python solution as an example: https://www.tutorialspoint.com/fizz-buzz-in-python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM