This is an example in a text book I'm studying from, and the line that follows the if statement is giving me confusing results. Shouldn't "if is_odds_on(bet[0]):" prevent everything but (75, 43, 60,) from proceeding to the next line? The exception in "perc_to_prob(bet[0])" is catching "111", why? Thank you.
"""Raising an exception during conversion, where the input cannot be converted."""
# Cut and pasted from a statistics program
def is_odds_on(percentage):
"""Determine whether a percentage probability is 'likely' or not"""
return (percentage > 50)
# Cut and pasted from a financial program
def expected_gains(amount_bet = 10, amount_to_win = 10, frac = 0.5):
"""For a given fractional chance of winning, and an amount bet, return
the most likely gains"""
return (amount_to_win * frac) - amount_bet
# Conversion methods - in both directions
def prob_to_perc(pr):
"""Convert a probability to a percentage"""
return pr * 100
def perc_to_prob(pc):
"""Convert a percentage to a probability, but raise exception if too big"""
if pc > 100:
raise ValueError("Percentage {0:g}% does not give a valid "
"probability".format(pc))
return pc / 100
# Execute this if the program is run directly
if __name__ == "__main__":
# Some input data
data = ( # Percentage change of winning, amount required to bet, amount you'll win
(23, 52, 274),
(75, 43, 60,),
(48, 118, 71,),
(111, 144, 159,), # Misprint in the percentage data!
)
# Work out how much we'd win if we only placed likely bets
total = 0
for bet in data:
if is_odds_on(bet[0]):
total += expected_gains(bet[1], bet[2], perc_to_prob(bet[0]))
print("Total gains: {0:g}".format(total))
Aucun commentaire:
Enregistrer un commentaire