Giter Site home page Giter Site logo

python-p3-countdown-to-midnight-lab's Introduction

Countdown to Midnight Lab

Learning Goals

  • Build a while loop.

Key Vocab

  • Interpreter: a program that executes other programs. Python programs require the Python interpreter to be installed on your computer so that they can be run.
  • Python Shell: an interactive interpreter that can be accessed from the command line.
  • Data Type: a specific kind of data. The Python interpreter uses these types to determine which actions can be performed on different data items.
  • Exception: a type of error that can be predicted and handled without causing a program to crash.
  • Code Block: a collection of code that is interpreted together. Python groups code blocks by indentation level.
  • Function: a named code block that performs a sequence of actions when it is called.
  • Scope: the area in your program where a specific variable can be called.

Review

This lab is going to test your skills in writing while loops. Remember, a while loop will execute your block of code only while your defined condition evaluates as True.

For example, this script:

x = 1
while x < 10:
    print(f'{x} is less than 10.')
    x += 1

Will print this:

1 is less than 10.
2 is less than 10.
3 is less than 10.
4 is less than 10.
5 is less than 10.
6 is less than 10.
7 is less than 10.
8 is less than 10.
9 is less than 10.

And return None.

f-Strings

Starting a string with f tells the Python interpreter that that string is going to be formatted with variable values. Passing variable names into an f-string with curly brackets {} interpolates that variable's value into the string.

Add-and-Assign (+=)

Add-and-assign is a shorthand used to increment the value of a numeric variable. It is very useful for while loops because it can push you toward your condition becoming False. (We don't want our loop to run forever!)

You can also subtract-and-assign (-=), multiply-and-assign (*=), and divide-and-assign (/=).


Instructions

This is a test-driven lab. Run pipenv install to create your virtual environment and pipenv shell to enter the virtual environment. Then run pytest -x to run your tests. Use these instructions and pytest's error messages to complete your work in the lib/ folder.

Write a function countdown() that takes in an integer argument and uses a while loop to countdown from that integer to 1, outputting f'{number} SECOND(S)!' in each iteration of the loop. The function should print() "HAPPY NEW YEAR!" after the loop finishes:

# => 10 SECOND(S)!
# => 9 SECOND(S)!
# => 8 SECOND(S)!
# => 7 SECOND(S)!
# => 6 SECOND(S)!
# => 5 SECOND(S)!
# => 4 SECOND(S)!
# => 3 SECOND(S)!
# => 2 SECOND(S)!
# => 1 SECOND(S)!
# => HAPPY NEW YEAR!

Our Python program executes so quickly that it doesn't actually count down at the speed of one second per number. Write a second function called countdown_with_sleep() that also takes one integer argument for the countdown and makes the loop pause for one second each trip around (hint).


Resources

python-p3-countdown-to-midnight-lab's People

Contributors

professor-ben avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.