212k views
0 votes
Write a function named predict_weather. This function takes in zero arguments and randomly returns a string describing the weather today. You should use random.randint() to generate a random number between 1 and 6, which you can then use to choose what weather condition to return.

1 Answer

7 votes

Final answer:

The function predict_weather randomly predicts the weather by utilizing random.randint() to select a weather condition from a predefined list.

Step-by-step explanation:

Creating the predict_weather Function. To write a function named predict_weather that randomly predicts the weather, we need to utilize the random module from Python's standard library. This function does not take any arguments and will generate a random weather condition based on a number obtained from using random.randint(). Here is how you can define the function:

import random

def predict_weather():
weather_conditions = ['sunny', 'rainy', 'cloudy', 'snowy', 'windy', 'stormy']
return weather_conditions[random.randint(0, 5)]
Each number between 1 and 6 corresponds to a different weather condition. When you call the function, it will return a string such as 'sunny' or 'rainy' based on the randomly generated number. This function can be called to get a random weather condition every time it is executed.

User Daprezjer
by
8.2k points