Skip to content

Instantly share code, notes, and snippets.

@popey456963
Last active March 6, 2016 21:15
Show Gist options
  • Save popey456963/d37d1696108f5216396a to your computer and use it in GitHub Desktop.
Save popey456963/d37d1696108f5216396a to your computer and use it in GitHub Desktop.
Corrections for Chapter 1

For Loops

for i in range(1, 10):
    print(i)

While Loops

i = 0

while n <= 5:
    print(i)
    i = i + 1

Else statements

i = 0

for i in range(15):
    a = i % 2
    b = i % 3
    if not a and not b:
        print("Popty Ping!")
    elif not a:
        print("Pop!")
    elif not b:
        print("Ping!")

Input

a = input("Int: ")
print(int(a)**0.5)

Pi

import math
print(format(math.pi, '.2f'))

Rounding

import math
print(round(math.pi, 3))

Char Code

char = "a"
charCode = ord(char)

They managed to get the chr code right...

Random

from random import randint
x = randint(1, 10)

Catching Errors

newInput = input("Int: ")
try:
    int(newInput)
except:
    print("Input Incorrect")

Largest Number

x, y = 17, 29
print("Largest number is: " + str(max(x,y))

def max(a, b):
    if a > b:
        return a
    else:
        return b

Square

ans = str(square(int(input("Int: "))))
def square(a):
    return a**2

Factorial

b = 4
print(factorial(b))
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment