Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions
#4
Some observations and suggestions:


- It's always a good practice to stick to conventions laid out in PEP8 - Function and Variable Names
- I know that in homework it's not always the case but in real life one should always try to generalise or abstract as much as possible. One way of doing it:

def convert_temperature(temperature, base='Celsius'):
    bases = ['Celsius', 'Fahrenheit']
    if base.title() not in bases:
         raise ValueError(f'Conversion base must one of values: {", ".join(bases)}')
    conversions = {'Celsius': lambda t: (t - 32) / 1.8,
                   'Fahrenheit': lambda t: (t * 1.8) + 32}
    return conversions[base](temperature)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Functions - by harold - Sep-11-2019, 03:55 AM
RE: Functions - by perfringo - Sep-11-2019, 06:16 AM
RE: Functions - by buran - Sep-11-2019, 06:40 AM
RE: Functions - by perfringo - Sep-11-2019, 06:57 AM
RE: Functions - by harold - Sep-11-2019, 07:06 AM
RE: Functions - by perfringo - Sep-11-2019, 07:43 AM

Forum Jump:

User Panel Messages

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