Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Device Boots Count | Scopes
Mastering Python: Closures and Decorators

book
Challenge: Device Boots Count

Aufgabe

Swipe to start coding

Imagine that you work in a company that produces devices. You have been tasked with developing a function that will count the number of boots on this device: with each boot, the device must increase the number of boots by 1.

  1. Define the boots variable in the global scope.
  2. Make changeable the boots global variable inside the boot() function local scope.
  3. Increase the boots variable by 1 inside the boot() function.
  4. If the device is booting for the first time, display a message:
    "The device boot for the first time".
  5. If the device is booting 2+ times, display a message: "The device boot N times", where N is a boots variable.

Lösung

boots = 0

def boot():
global boots
boots += 1
if boots == 1:
print("The device boot for the first time")
else:
print("The device boot", boots, "times")

boot()
boot()
boot()

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
___ = 0

def boot():
___ boots
boots += 1
if boots == 1:
print("___")
else:
print("The device boot", ___, "times")

boot()
boot()
boot()

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt