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)
jjk
  • 1
  • 1
    Does this answer your question? [What is the correct syntax for 'else if'?](https://stackoverflow.com/questions/2395160/what-is-the-correct-syntax-for-else-if) – kaya3 Sep 13 '22 at 02:12

1 Answers1

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.

atigdawgahb
  • 41
  • 1
  • 5