mercredi 28 juillet 2021

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 :)

Aucun commentaire:

Enregistrer un commentaire