aemc
aemc

Reputation: 35

How to find how many user inputs are the same?

Determine how many numbers are equal. Print that number (zero, two, or three) along with some descriptive text to inform the user of what is being printed to the screen

This only works sometimes and I know that there must be a more efficient way but I am having trouble thinking of something else since I just started learning python.

#Taking the inputs from the user 
x,y,z = input("Enter the three valaues: ").split()

while True:
    if x!= y and x!=z:
      print("there is no equal numbers")
      break
    elif x==y or x==z:
      print("There are two equals numbers")
      break 
    else 
      print("There are three equals numbers")

Upvotes: 1

Views: 456

Answers (3)

Timur Shtatland
Timur Shtatland

Reputation: 12395

Use sets to find the count of equal numbers:

lst = input("Enter 3 values, delimited by blanks: ").split()
num_equal = len(lst) - len(set(lst))
if num_equal:
    num_equal += 1
print(f'There are {num_equal} equal numbers')

Upvotes: 2

ddejohn
ddejohn

Reputation: 8962

Since you're dealing with 3 values, a slightly different approach than the other two answers would be to use a set and a dict:

num_equal = 3 - len({x, y, z})
print({0: "No equal numbers", 1: "Two equal numbers", 2: "All three equal"}[num_equal])

Upvotes: 0

Selcuk
Selcuk

Reputation: 59405

You missed the case where y == z but x != y.

A more readable implementation would be:

if x == y == z:
    print("Three equal numbers")
elif x != y and y != z and x != z:
    print("No equal numbers")
else:
    print("Two equal numbers")

Also, your while should come before your input line so that it asks the user a new set of numbers.


Upvotes: 2

Related Questions