Skip to main content

Posts

Showing posts with the label function

Python — Simplifying Decorators

🐍 Decorators can be complex. But if you study the following, you will understand it in the best form. Step 1: Create Function Input def my_function(): print('Step 1: Create first function') my_function() Output Step 1: Create first function Step 2: Create Decorator Input def my_decorator(func): print('Step 2: Create a decorator. A decorator is a function') func() print('Step 3: Call first function') @my_decorator def my_function(): print('Step 1: Create first function') my_function() Output Step 2: Create a decorator. A decorator is a function Step 1: Create first function Step 3: Call first function Step 3: Add a function and  decorator within the decorator Input import functools def my_decorator(func): @functools.wraps(func) def func_that_runs_func(): print('Step 2: Create a decorator. A decorator is a function') func() print('Step 3: Call first function...

Python - Calculate Number of Tiles Required And Total Expenditure

🐍  In Python, calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user. Solution def tile_calculator(roomlen, roomwid, tilelen, tilewid, tileprice):          if roomlen!=0 and roomwid!=0 and tilelen!=0 and tilewid!=0:                  if roomlen%tilelen==0:             notileslen = int(roomlen//tilelen)                          if roomwid%tilewid==0:                 notileswid = int(roomwid//tilewid)                          else:                 notileswid = int((roomwid//tilewid)+1)                  elif roomlen%tilewid==0:             ...