based on the test script that you provided, you can try something like this:
from dataclasses import dataclass
@dataclass
class Weather:
temperature: int
rainfall: int
is_sunny: bool
# Create four instances of the class 'Weather'
day1 = Weather(temperature=56, rainfall=0, is_sunny=True)
day2 = Weather(temperature=85, rainfall=0, is_sunny=True)
day3 = Weather(temperature=68, rainfall=0, is_sunny=True)
day4 = Weather(temperature=77, rainfall=0, is_sunny=True)
def days_until_raining(reports: list[Weather]) -> int:
non_rainy_days = 0
found_rainy_day = False
for report in reports:
if report.rainfall > 0:
found_rainy_day = True
#elif not found_rainy_day: # Makes sense if 'else' is used instead
else:
non_rainy_days += 1
return non_rainy_days
# Pass the four days as arguments to the function in list format
result = days_until_raining([day1, day2, day3, day4])
print(f'Total of non-rainy days is: {result}')
Why this is necessary? It’s never used (except possibly changing its value). I am also somewhat puzzled what the objective is.
If the objective is to count non-rainy days then simple approach can be used (assuming that rainfall 0 means that there is no rainfall):
from dataclasses import dataclass
@dataclass
class Weather:
temperature: int
rainfall: int
is_sunny: bool
day1 = Weather(temperature=56, rainfall=0, is_sunny=True)
day2 = Weather(temperature=85, rainfall=0, is_sunny=True)
day3 = Weather(temperature=68, rainfall=1, is_sunny=True)
day4 = Weather(temperature=77, rainfall=0, is_sunny=True)
days = [day1, day2, day3, day4]
non_rainy = sum(not day.rainfall for day in days)
# non-rainy = 3
yes, you are right about this. It is not necessary. However, from the OPs original post,
he stated that they are "new to programming" and from the post’s title "How to print data from a function", I figure I would answer the question at hand based on the script that was provided without having to critique the script much further. If they continue on their Python quest, through practice, trial and error, they will surely make this discovery on their own.
May I trouble you with another question?
Same thing, different code. How do I use the function count_pets on the data in south_weber?
I’m learning dataclasses and functions but don’t quite understand how to use them or what to do with a small amount of data.
Thanks in advance,
Kerry
from dataclasses import dataclass
@dataclass
class Person:
name: str
pets: int
@dataclass
class House:
address: str
owner: Person
@dataclass
class Neighborhood:
name: str
houses: list[House]
def count_pets(neighboorhood: Neighborhood) -> int:
pets = 0
houses = neighboorhood.houses
for house in houses:
pets += house.owner.pets
return pets
south_weber = Neighborhood("Cedar Bluff", [
House('1805 E 7600 S', Person("Kerry", 2)),
House('1815 E 7600 S', Person('Kurt', 0)),
House('1855 E 7600 S', Person('Randy', 3)),
House('1905 E 7600 S', Person("Terry", 4)),
House('1915 E 7600 S', Person('Burt', 1)),
House('1955 E 7600 S', Person('Andy', 3)),
])
# Passing in instance as an argument to the function
print(count_pets(south_weber))
I understand that you’re anxious to dive right in with learning Python. However, you will be better served if you learn the fundamentals prior to jumping head first into advanced topics including decorators, classes, and object oriented programming (OOP) in general. There are a lot of nuances that you really have to understand to fully appreciate these example script snippets that you are working with (including functions as you pointed out).
Here is a tutorial regarding the @dataclass decorator and a link to its documentation. Notice that understanding this decorator in particular and decorators in general will really help you with understanding this code snippet (among other topics of course).
If you really want to learn Python the “right way”, I highly recommend the following book whose latest edition is right around the corner. I have used the 5th edition and looking forward to this latest edition. Best book out on the market for Python, I’ll say.