A Chance of Rain
Intermediate · Write the code · from Random Values and Simulations: RANDOM(a, b) in Python
From the lesson: Random Values and Simulations: RANDOM(a, b) in Python
A random value is a number a program cannot predict ahead of time, and simulations use random values to copy the uncertainty of the real world, from a coin toss to the way a rumor travels through a school. Games use them to shuffle cards, and weather apps use them to try out thousands of possible tomorrows.
Read the lessonWrite chance(percent) that returns True about percent percent of the time and False the rest of the time. Pick a random whole number from 1 to 100 and compare it with percent.
chance(0) must never return True, and chance(100) must always return True.
What Check runs
Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| found_true = False for _ in range(1000): if chance(0): found_true = True print(found_true) | False |
| always = True for _ in range(1000): if not chance(100): always = False print(always) | True |
More Simulation exercises
- Roll Any DieBeginner
- Board Game TurnsAdvanced