user3396351 - 10 months ago 165

Python Question

`import math`

x = int(input("Enter a value for x: "))

y = int(input("Enter a value for y: "))

if y == 1 or y == x:

print(1)

if y>x:

print(0)

else:

a = math.factorial(x)

b = math.factorial(y)

div = (a//(b*(x-y)))

print(div)

This binomial coeeficient program works but when i input two of the same number which is supposed to equal to 1 or when y is greater than x it is supposed to equal to 0. the program needs a little tweaking if someone can please help me

Answer

Your program will continue with the second `if`

statement in the case of `y == x`

, causing a `ZeroDivisionError`

. You need to make the statements mutually exclusive; the way to do that is to use `elif`

("else if") instead of `if`

:

```
import math
x = int(input("Enter a value for x: "))
y = int(input("Enter a value for y: "))
if y == x:
print(1)
elif y == 1: # see georg's comment
print(x)
elif y > x: # will be executed only if y != 1 and y != x
print(0)
else: # will be executed only if y != 1 and y != x and x <= y
a = math.factorial(x)
b = math.factorial(y)
c = math.factorial(x-y) # that appears to be useful to get the correct result
div = a // (b * c)
print(div)
```