Results for “python def”

Showing 21–30 of 71

  1. Article Mini projects

    Calculate your weight on different planets in Python

    …dictionary where you have a word (the key) and its definition (the value). In our program, we use a dictionary to store planets (keys) and their corresponding surface gravities (values). This allows…

  2. Article Python Fundamentals

    Python print() Function

    …readings temperature = 21 pressure = 101.3 oxygen = 98 # Default separation (space) print("Temperature:", temperature, "Pressure:", pressure, "Oxygen:", oxygen) # Output: Temperature: 21 Pressure…

  3. Article Mini projects

    Medical dispenser - Don’t trust the user

    …"""The Hail Mary's original dispenser - easily hackable""" def __init__(self): self.dose_history = [] self.minimum_interval = timedelta(hours=3) self.max_daily_doses = 6 def request_dose(self…

  4. Article Python Fundamentals

    Python If/Else for Beginners

    …numeric" Short, practical example — simple mission planner def launch_decision(fuel, weather, comms): if fuel < 40: return "Abort: low fuel" elif not comms: return "Abort: communications offline" elif…

  5. Article Python Fundamentals

    Variables and data types in Python

    …keywords include: False class finally is return None def for lambda try True del from nonlocal while and elif global not with as else if or yield break except import pass Example of valid variable…

  6. Challenge

    Launch counter crash

    record_launch() adds 1 to a global counter and stops with an UnboundLocalError. Predict it, find out why Python treats the name as local, fix it by passing the count in and returning the new one, then…

    Fix the code Medium 3 stages functions scope UnboundLocalError global
  7. Challenge

    Read the traceback

    A Python program stops with a traceback. Read it from the bottom up to find the error, the function and the line, fix the bug there, then follow a longer traceback through four calls to a second bug.

    Fix the code Easy 2 stages errors traceback KeyError TypeError
  8. Challenge

    Confirm by name

    …to give by accident. Keyword-only and required Change the def line to abort(reason, *, confirm) , so confirm can only be passed by name and can't be left out, and return the refusal when it isn't…

    Write the code Medium 2 stages functions keyword-only arguments * safety