Pretty simple bit of code that'll solve a square root. The original formula says estimate but from my experience testing this it should solve it, although sometimes it has to be rounded. Pretty much it'll run the formula again and again until the result of it is the same more than once, which means it can't get any closer to the answer AKA it is the answer.
N = float(input("Number: "))
E = 1
E2 = 1
while True:
E = (E + N / E) / 2
if E2 == E:
break
E2 = E
print(E)
print(E * E)
A text based connect 4 game
#instructions
print("Welcome to connect 4!")
print("Type the number above the row to place a tile")
#Initializing variables
win = False
heights = [ 5,5,5,5,5,5,5 ]
columncount = 7
rowcount = 6
columns = [["-" for _ in range(rowcount)] for _ in range(columncount)]
turn = "X"
#Game board
def printBoard():
print ()
print ("1 2 3 4 5 6 7")
for i in range(rowcount):
for j in range(columncount):
print(columns[j][i], end=' ')
print()
while win == False:
printBoard()
print () #Blank line to make output neater
print (turn,"'s turn")
place = input ("Place?")
try:
place = int(place) - 1
if place < 0 or place >= columncount or heights[place] >= rowcount:
print("Invalid input") #Check if it is a valid place
continue
columns[place][heights[place]] = turn
heights[place] = heights[place] - 1 #place tile
except ValueError:
print("Invalid input")
if turn == "X":
turn = "O"
else:
turn = "X" #change turn (so turn change at end will revert to original)
#Check for wins
#Check for horizontal wins
for i in range(columncount):
for j in range(rowcount - 3):
if columns[i][j] == "X" and columns[i][j+1] == "X" and columns[i][j+2] == "X" and columns[i][j+3] == "X":
printBoard()
print("X wins!")
win = True
elif columns[i][j] == "O" and columns[i][j+1] == "O" and columns[i][j+2] == "O" and columns[i][j+3] == "O":
printBoard()
print("O wins!")
win = True
#Check for vertical wins
for j in range(rowcount):
for i in range(columncount - 3):
if columns[i][j] == "X" and columns[i+1][j] == "X" and columns[i+2][j] == "X" and columns[i+3][j] == "X":
printBoard()
print("X wins!")
win = True
elif columns[i][j] == "O" and columns[i+1][j] == "O" and columns[i+2][j] == "O" and columns[i+3][j] == "O":
printBoard()
print("O wins!")
win = True
#Check for diagonal wins
for i in range(columncount):
for j in range(rowcount):
if i + 3 < columncount and j + 3 < rowcount and \
columns[i][j] == "X" and columns[i+1][j+1] == "X" and columns[i+2][j+2] == "X" and columns[i+3][j+3] == "X":
printBoard()
print("X wins!")
win = True
if i + 3 < columncount and j + 3 < rowcount and \
columns[i][j] == "O" and columns[i+1][j+1] == "O" and columns[i+2][j+2] == "O" and columns[i+3][j+3] == "O":
printBoard()
print("O wins!")
win = True
if i + 3 < columncount and j - 3 < rowcount and \
columns[i][j] == "X" and columns[i+1][j-1] == "X" and columns[i+2][j-2] == "X" and columns[i+3][j-3] == "X":
printBoard()
print("X wins!")
win = True
if i + 3 < columncount and j - 3 < rowcount and \
columns[i][j] == "O" and columns[i+1][j-1] == "O" and columns[i+2][j-2] == "O" and columns[i+3][j-3] == "O":
printBoard()
print("O wins!")
win = True
#Check for draws
draw = True
for i in range(columncount):
for j in range(rowcount):
if columns[i][j] == "-":
draw = False
#Switch turn
if draw == True:
win = True
printBoard()
print("Draw!")
if turn == "X":
turn = "O"
else:
turn = "X"
Quote from Wikipedia: "The Collatz conjecture is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1. It concerns sequences of integers in which each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that these sequences always reach 1, no matter which positive integer is chosen to start the sequence. The conjecture has been shown to hold for all positive integers up to 2.95×10, but no general proof has been found."
This code simply runs through that formula for numbers 1-20 (just a random number i picked, it can be changed pretty easily to use a difference range of numbers by editing the code on line 4) and lists each outcome, as you can see they all end in 1 as predicted by the conjecture.
Of course I didn't find a proof for the conjecture, it just runs through the formula. This is just some coding practice based off something I found interesting. I got the idea from a video essay about the topic that i watched on YouTube.
CClists = []
j = 0
for i in range(1, 21):
j = i
CClist = []
while j != 1:
CClist.append(j)
if j % 2 == 0:
j = j // 2
else:
j = j * 3 + 1
CClist.append(j)
CClists.append(CClist)
for x in range (len(CClists)):
print(CClists[x])
The classic 12 days of Christmas coding challenge
gifts = [
"Twelve drummers drumming", "Eleven pipers piping", "Ten lords a-leaping",
"Nine ladies dancing", "Eight maids a-milking", "Seven swans a-swimming",
"Six geese a-laying", "Five golden rings", "Four calling birds",
"Three french hens", "Two turtle doves", "And a partridge in a pear tree"
]
print("The 12 days of christmas")
for i in range (13):
foo = "th"
if i == 1: foo = "st"
if i == 2: foo = "nd"
if i == 3: foo = "rd"
if i>0: print("On the",i,foo,"day of christmas, my true love gave to me")
h = len(gifts)-i
for j in range(i):
if i == 1:
print("A partridge in a pear tree")
else:
print(gifts[h])
h+=1
print()
print(":D")
A game I played in primary school as well as a coding challenge. You pick a number (i'll call it x) and start counting, if the number is divisible by x you replace it with fizz, if the number contains the digit x then you replace it with buzz, and if the number is both then you replace it with fizzbuzz.
import math
def dig (number, digit):
return str(digit) in str(number)
n = int(input("Pick a number: "))
r = int(input("Count to...? "))
c=0
for i in range(r):
c=c+1
if c % n ==0 & dig(c, n):
print("fizzbuzz")
elif c % n ==0:
print ("fizz")
elif dig(c, n):
print("buzz")
else:
print (c)
# n=number c=count r=repeat
Something i just made today because i was bored, very simple just picks a random answer. I did notice that the original 8ball has more positive answers which makes it more likely to give a positive answer so i made it so there's an equal chance between each answer group.
import random
pos = ['It is certain','It is decidedly so','Without a doubt',\
'Yes definitely','You may rely on it','As I see it, yes',\
'Most likely','Outlook good','Yes','Signs point to yes']
idk = ['Reply hazy, try again','Ask again later','Better not tell you now',\
'Cannot predict now','Concentrate and ask again']
neg = ['Don’t count on it','My reply is no','My sources say no',\
'Outlook not so good','Very doubtful']
answers = [pos,idk,neg]
rand = (random.randint(1,3))
variableThatDoesntDoAnything = input('Ask a question: ')
print(answers[rand][random.randint(1,len(answers[rand]))])