0

My code below passes all tests with the exception of when 2 or 3 of the numbers are equal. In that case, my output is showing 2 or 3 of the same number, i.e. 7 three 3 times if x y and z are all equal to 7. Any ideas on how to stop that from happening? # Input 3 variables # Use conditional expressions to print the smallest integer # Code should work even if 2 or 3 of the numbers are equal

x = int(input("Insert integer"))
y = int(input("Insert integer"))
z = int(input("Insert integer"))

if x <= y and x <= z:
    print(x)

if y <= x and y <= z:
    print(y)
    
if z <= x and z <= y:
    print(z)
1

1 Answer 1

1

If all numbers are equal all 3 conditions will be satisfied and will print all 3 numbers. To filter the condition you have to use if elif else if elif else

x = int(input("Insert integer"))
y = int(input("Insert integer"))
z = int(input("Insert integer"))

if x <= y and x <= z: # x is smallest(if true) and print x if false move to next elif
    print(x)

elif y <= x and y <= z: # y is smallest(if true) and print y if false move to next elif
    print(y)
    
else: # z is smallest(if true) and print z
    print(z)

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.