Exercise 4: Explore Generators
Practice writing a fake weather prediction generator.
We'll cover the following
Problem statement
This chapter explains how to write functions that return
generators and multiple values. First, consider how to generate the two random values. Evaluate the conditions they represent for being harsh and choose accordingly: return
or yield
.
Task
Write a function that returns a "weather Oracle"
, similar to the fortune teller in the lesson "yield,
do not return."
The Oracle returns a random integer temperature from -89°C to 57°C (not inclusive) and a random integer humidity in the range from zero to 100 (inclusive). The generator stops either after the pre-specified number of attempts or when the generated weather conditions are too harsh (when temperature < -40°C and humidity <= 50), whichever comes first.
Get hands-on with 1400+ tech skills courses.