Please excuse my beginners' mistakes :) I have a task that asks me to find probabilities for user input amounts of total lottery balls and balls drawn. There's most likely something else wrong with my code as well, but I can't get past the error code that says n is not defined. This is what I have:
import math
def winning(n, p):
"""Find the probability of guessing all balls correct"""
a = math.factorial(n)/(math.factorial(n - p) * math.factorial(p))
b = 1/a
return b
win = winning(n, p)
def main():
n = int(input("Enter the total number of lottery balls:", ))
# number of balls
p = int(input("Enter the number of the drawn balls:", ))
# number of balls drawn
if p > n:
print("At most the total number of balls can be drawn.")
if n < 1:
print("The number of balls must be a positive number.")
else:
print("The probability of guessing all", p, "balls correctly is", win)
main()
Also, is math.factorial() a working way to use factorials in Python or should I use something else?
CodePudding user response:
You are making a call to winning(n, p) without n or p having been defined beforehand. As Tim mentions, moving your call to winning(n, p) to after your main() definition would mean it is called AFTER n and p are declared by the user.
For example the following produces your error:
def sum(a, b):
return (a b)
sum(a, b)
a = int(input('Enter 1st number: '))
b = int(input('Enter 2nd number: '))
whereas the following does not:
def sum(a, b):
return (a b)
a = int(input('Enter 1st number: '))
b = int(input('Enter 2nd number: '))
sum(a, b)
CodePudding user response:
How about utilizing math.comb
to calculate the binomial coefficient instead of calculating it manually:
"""Lottery Probability Calculator."""
import math
import numpy as np
def get_lottery_coverage(total_balls: int, balls_drawn: int) -> int:
return math.comb(total_balls, balls_drawn)
def get_int_input(prompt: str) -> int:
while True:
try:
return int(input(prompt))
except ValueError:
print('Error: Enter a integer, try again...')
def main() -> None:
print('Lottery Probability Calculator')
print('==============================')
total_balls = get_int_input('Enter the total number of lottery balls: ')
if total_balls < 1:
print('Error: The number of lottery balls must be a positive number.')
balls_drawn = get_int_input('Enter the number of balls drawn: ')
if balls_drawn < 1:
print('Error: The number of balls drawn must be a positive number.')
if balls_drawn > total_balls:
print(f'Error: At most {total_balls} can be drawn.')
print(f'The probability of guessing all {balls_drawn} balls correctly is:')
total_coverage = get_lottery_coverage(total_balls, balls_drawn)
win_probability = 1 / total_coverage
win_probability_percentage = win_probability * 100
print(f'{np.format_float_positional(win_probability, trim="-")}')
print('or')
print(f'{np.format_float_positional(win_probability_percentage, trim="-")}%')
print('or')
print(f'1 in {total_coverage:,}')
if __name__ == '__main__':
main()
Example Usage:
Lottery Probability Calculator
==============================
Enter the total number of lottery balls: 49
Enter the number of balls drawn: 6
The probability of guessing all 6 balls correctly is:
0.00000007151123842018516
or
0.000007151123842018516%
or
1 in 13,983,816