Python Forum
How to build this code ?? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to build this code ?? (/thread-3810.html)

Pages: 1 2


How to build this code ?? - aquila - Jun-26-2017

I would like some HELP!!

I want to make a simple noob function before put into my django project.

I want a function that provides the lamp power of a room.

Input: The room's area (m²)
Output: The lamp power (W).

The rules of this math is:
If the entry area is <= 6m² then the power will be 100W (easy).
If the area is > 6m² then for the first 6m² the power is 100W, BUT for the next 4m² is added 60W to the lamp power.

Ex: Area = 20m²
6m² + 4m² + 4m² + 4m² + 2m² = 20 m²
100 + 60 + 60 + 60 + 0** = 280 W

**(less than exact 4m² doesn't count)


def light(area):
    power = 0
    if area < 6:
        power = 100
    else:
        ........

#testing
print(luz(20))
Any help to make this simple code would be AWESOME!


RE: How to build this code ?? - micseydel - Jun-27-2017

What have you tried?


RE: How to build this code ?? - Ofnuts - Jun-27-2017

(Jun-27-2017, 12:54 AM)micseydel Wrote: What have you tried?

Not LED lights, obviously :)


RE: How to build this code ?? - DeaD_EyE - Jun-28-2017

Try this one:

def light(area):
    if area <= 6:
        return 100
    else:
        new_area = area - 6
        return 100 + (new_area // 4) * 60
I'm not sure if the calculation is right. The double slash is integer division in Python 3.x.
Take focus on the else-clause.


RE: How to build this code ?? - buran - Jun-28-2017

(Jun-26-2017, 11:38 PM)aquila Wrote: #testing
print(luz(20))

well, this will result in
Error:
Traceback (most recent call last): File "C:\luz.py", line 9, in <module> print(luz(20)) NameError: name 'luz' is not defined >>>
:-)

def light(area):
    if area > 0:
        return 100 + max(0,(area-6)) // 4 * 60
    else:
        raise ValueError('Room area must be positive number. Received {}.'.format(area))



RE: How to build this code ?? - DeaD_EyE - Jun-28-2017

Read the stacktrace and compare the name of the function.

def light(area):


RE: How to build this code ?? - buran - Jun-28-2017

(Jun-28-2017, 01:24 PM)DeaD_EyE Wrote: Read the stacktrace and compare the name of the function.

def light(area):

Is this reply to my post?


RE: How to build this code ?? - DeaD_EyE - Jun-28-2017

Yes and as an addition for the original post. He should learn to read and understand a stacktrace.


RE: How to build this code ?? - buran - Jun-28-2017

(Jun-28-2017, 01:30 PM)DeaD_EyE Wrote: He should learn to read and understand a stacktrace.
I misinterpreted your post :-)


RE: How to build this code ?? - aquila - Jun-28-2017

Thanks!

This here worked!

def light(area):
       if area <= 6:
          return 100
       area = area - 6
       power = 100
       temp = int(area) // 4
       power = power + temp * 60
       return power


print(light(20)) #this must return 280
print(light(11.05))
print(light(11.43))
print(light(22))
print(light(5.8))
print(light(19))