Python Projects

Various projects built in Python.

Python Projects

The following programmes are examples that I have created as part of further learning for Python. These are all simplistic programmes with little to no error checking although this could be added to most to ensure the inputs given were of the correct type and within a given range.

Fibonacci Sequence

This programme asks the user for a number and returns the value in the Fibonacci sequence at that corresponding location.
print('This program will run the Fibonacci sequence to the Nth iteration.')
n = input('Enter a number for N: ')
x = 0
y = 1
z = 0
steps = 1

while steps < int(n):
    z = x + y
    x = y
    y = z
    steps += 1

print('The {}th Fibonacci term is: {}'.format(n,z))

Coin Flip Simulation

This programme asks the user for a number and flips that many coins. The programme then returns a statement to the screen of how many heads and how many tails occured.
import random
print('This program will flip a given number of coins and return the results.')
n = input('Enter the number of coins to flip:')
y = 0
z = 0
heads = 0
tails = 0

def coin_flip():
    y = random.randint(1,2)
    return y

for x in range(1, int(n) + 1):
    z = coin_flip()
    if z == 1:
        heads += 1
    elif z == 2:
        tails += 1

print('Flipped {} coins returning {} heads and {} tails'.format(n,heads,tails))

Collatz Sequence

This programme takes a number given by the user and passes it throught the Collatz sequence and returns the number of steps required to reach 0.
print('This program will run the Collatz sequence on a given number and return the number of steps required to hit 0.')
n = input('Enter a number to run in the Collatz sequence:')
n = int(n)

def collatz(x,steps):
    while x > 1:
        if x % 2 == 0:
            x = x / 2
            steps += 1
            x = (x * 3) + 1
            steps += 1
    return steps

z = collatz(n,0)

print('There are {} steps required to reach 0 starting from {}.'.format(z,n))