Skip to content

Instantly share code, notes, and snippets.

@MggMuggins
Last active December 15, 2017 22:23
Show Gist options
  • Save MggMuggins/6b89ac3c6a6bd3ae05c84f15b5a2a2b6 to your computer and use it in GitHub Desktop.
Save MggMuggins/6b89ac3c6a6bd3ae05c84f15b5a2a2b6 to your computer and use it in GitHub Desktop.
Python Cheat Sheet for K-8 Coding night

Here are some example problems to solve! They are listed roughly by how hard they will be to solve, top to bottom (easiest at the top).

Random Numbers

This isn't a challenge, just something that is used in a number of the other challenges. How to create a random number:

# Allows us to use a function someone else wrote (called "randint")
from random import randint

# Selects a number anywhere between 6 and 12 (including 6 and 12)
my_var = randint(6, 12)

Dice Rolling Simulator

When the program runs, it will randomly choose a number between 1 and 6. (Or whatever other integer you prefer — the number of sides on the die is up to you.) The program will print what that number is. It should then ask you if you’d like to roll again.

Coin Flipper

Flips a coin! (Hint, you'll need to use the randint function here too)

Change Return

Have the user enter an price, and the amount of cash the customer has given, and have it calculate the change (could put this in a loop so you don't have to run the program many many times).

You could also add sales tax, so that the user inputs a subtotal (total without tax), and the program calcuates the tax as well.

Area Calculator

When the program runs, it will prompt the user for a length and width, and then calculate the area of the resulting rectangle. You can expand this program to include multiple shapes, or multiple measurments (things like perimeter)

You could also have a user input the area of a floor space that needs to be covered with tile, and given the size of a single tile, make the program calculate how much tile is needed to cover the floor (could also be adapted to carpet, or even volume, aka, the amount of dirt needed to make a layer that is a certain depth over a specified area)

Fibonacci Sequence

A Fibonacci sequence is a series of numbers that are the sum of the previous two numbers of the sequence. For example: 1, 1, 2, 3, 5, 8, 13 is a fibonacci sequence, since 1 + 1 = 2, and 1 + 2 = 3, and so on. Create a program that generates a certain number of fibbonacci numbers (inputted by the user of course!). Alternatively, you could generate a fibonacci sequence based off of a user-inputted number (for example, input 4, and build this sequence: 4, 4, 8, 12, 20 etc.)

Fizz Buzz

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Guess the Number

The program will first randomly generate a number unknown to the user. The user needs to guess what that number is. (In other words, the user needs to be able to input information.) If the user’s guess is wrong, the program should return some sort of indication as to how wrong (e.g. The number is too high or too low). If the user guesses correctly, a positive indication should appear (And the program should exit).

You can use functions to check if the user input is an actual number, to see the difference between the inputted number and the randomly generated numbers, and to then compare the numbers.

Hint, the exit() function can be used to quit the program.

Guess the Word - Hard!

The program will need a user to input a single letter guess and if the letter is in the word, then give some indication of that. This game is similar to hangman, but instead of a hangman you can use a counter someplace in your program.

Hint, You'll need something called a for loop. One of your room instructors can tell you about them.

Output

print("Hi")

Input

my_var = input("Here is the prompt: ")

Variables/Data Types

  • Integer - A number, can only be whole numbers
  • Decimal - A number, can be whole and decimal numbers (also called a "float")
  • Bool - A Boolean (Aka, True or False)
  • String - A bunch of characters
my_integer = 8
float_or_decimal = 7.3
my_bool = True
a_string = "Hello, I'm a string!"

Numerical Operations

  • == - Equals; True it both sides of the expression are the same
  • != - Not-Equals; True if the sides of the expression are Not the same
  • <, > - Less-than and Greater-than; These work the same way as in math
  • <=, >= - Less-than or equal-to and Greater-than or equal-to; These also work the same way as math
if 1 == 1:
  # I Execute!

if 1 != 57:
  # I Execute!

if 3 < 9:
  # I Execute!

if 8 <= 8:
  # I Execute!

Boolean Operations

  • not - True if the following expression is False
  • and - True if expressions on both sides are true
  • or - True if one of the expressions on either side are true, but not both
if not False:
  # I execute!

if True and True:
  # I Execute!

if True or False:
  # I Execute!

Loops

While runs it's content over and over again until the boolean expression evaluates to False

while True:
  # I Run forever!
guess = input("Input a guess: ")
while guess.lower() != "hi":
  guess = input("Input a guess: ")

print("Yay, you made it!")

Functions

A function allows you to split up your code into smaller parts and prevent code duplication.

So instead of the example above, you could write a function that makes the "Input a Guess: " lines be a function, that way you only have to change one spot in your code instead of two:

def get_guess():
  return input("Input a guess: ")

guess = get_guess()
while guess.lower() != "hi":
  guess = get_guess()

print("Yay, you made it!")

Methods

Methods are functions that are associated with objects. Different objects have different methods. This is a feature of classes, which we aren't covering here, but there are lots of resources online.

For example, a String data type is an object, and strings have a lower() method associated with them, which returns a new string that is that string, but all lower case. Methods are called like functions with Parens (), but they use a dot as well, since they are called on a specific variable.

Link to the python presentation
https://docs.google.com/a/myunit5.org/presentation/d/1yrUGis8SGQqtwVlPXftLVsol9J18tVvaFvnmCYxsK6g/edit?usp=sharing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment