Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to build this code ??
#1
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!
Reply
#2
What have you tried?
Reply
#3
(Jun-27-2017, 12:54 AM)micseydel Wrote: What have you tried?

Not LED lights, obviously :)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(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))
Reply
#6
Read the stacktrace and compare the name of the function.

def light(area):
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(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?
Reply
#8
Yes and as an addition for the original post. He should learn to read and understand a stacktrace.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
(Jun-28-2017, 01:30 PM)DeaD_EyE Wrote: He should learn to read and understand a stacktrace.
I misinterpreted your post :-)
Reply
#10
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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,193 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Working code to build GPX from geotagged JPGs? Winfried 3 2,683 May-10-2020, 08:58 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020