Exercise 8: Fishing in the Nordics, advanced
Hello. I’m having problems with the Exercise 8: Fishing in the Nordics, on advanced level.
my code is:
import random
countries = ['Denmark', 'Finland', 'Iceland', 'Norway', 'Sweden']
populations = [5615000, 5439000, 324000, 5080000, 9609000]
male_fishers = [1822, 2575, 3400, 11291, 1731]
female_fishers = [69, 77, 400, 320, 26]
def guess(winner_gender):
if winner_gender == 'female':
fishers = female_fishers
else:
fishers = male_fishers
# write your solution here
tries = 10000
result = []
total = sum(fishers)
for i in range(tries):
rv = random.randint(1, total)
i = 0
for c in fishers:
if rv <= c:
break
rv -= c
i += 1
result.append(i)
final = [[-result.count(i), i] for i in range(len(fishers))]
final.sort()
guess = countries[final[0][1]]
biggest = -final[0][0] / tries * 100
return (guess, biggest)
def main():
country, fraction = guess("male")
print("if the winner is male, my guess is he's from %s; probability %.2f%%" % (country, fraction))
country, fraction = guess("female")
print("if the winner is female, my guess is she's from %s; probability %.2f%%" % (country, fraction))
main()my output:
if the winner is male, my guess is he's from Norway; probability 54.54%
if the winner is female, my guess is she's from Iceland; probability 45.52%
When testing the code, I get this error:
Test Failed: AssertionError: False is not true : Incorrect answer. Please check your code.
The correct propabilities I calculated are:
0,54234113… and 0,4484304…
Increasing tries up to 100 000 000 didn’t help
Any help is appreciated (: