My code for Tic-Tac-Toe (beginner), compared to my mate that works at google's code.
Thought it would be interesting seeing someone beginner code, vs someone's professional code on the same project. After I sent him my code he replied with how he would have done it, as a comparison.
**My code:**
import random
def plyr1wintest(): #checks if player has 3 in a row
global winner
if (1 in plyr1list) and (2 in plyr1list) and (3 in plyr1list) or \
(4 in plyr1list) and (5 in plyr1list) and (6 in plyr1list) or \
(7 in plyr1list) and (8 in plyr1list) and (9 in plyr1list) or \
(1 in plyr1list) and (4 in plyr1list) and (7 in plyr1list) or \
(2 in plyr1list) and (5 in plyr1list) and (8 in plyr1list) or \
(3 in plyr1list) and (6 in plyr1list) and (9 in plyr1list) or \
(1 in plyr1list) and (5 in plyr1list) and (9 in plyr1list) or \
(3 in plyr1list) and (5 in plyr1list) and (7 in plyr1list):
print ((name1) + ' wins as X')
winner = True
def plyr2wintest(): #checks if player has three in a row
global winner
if (1 in plyr2list) and (2 in plyr2list) and (3 in plyr2list) or \
(4 in plyr2list) and (5 in plyr2list) and (6 in plyr2list) or \
(7 in plyr2list) and (8 in plyr2list) and (9 in plyr2list) or \
(1 in plyr2list) and (4 in plyr2list) and (7 in plyr2list) or \
(2 in plyr2list) and (5 in plyr2list) and (8 in plyr2list) or \
(3 in plyr2list) and (6 in plyr2list) and (9 in plyr2list) or \
(1 in plyr2list) and (5 in plyr2list) and (9 in plyr2list) or \
(3 in plyr2list) and (5 in plyr2list) and (7 in plyr2list):
print ((name2) + ' wins as O')
winner = True
def printboard(): #print board
print(' ')
print(' '+ position[0] +' | '+ position[1] +' | '+ position[2] + ' ' + ' '+ '1' +' | '+ '2' +' | '+ '3')
print('-----------' + ' ' + '-----------')
print(' '+ position[3] +' | '+ position[4] +' | '+ position[5] + ' ' + ' '+ '4' +' | '+ '5' +' | '+ '6')
print('-----------' + ' ' + '-----------')
print(' '+ position[6] +' | '+ position[7] +' | '+ position[8] + ' ' + ' '+ '7' +' | '+ '8' +' | '+ '9')
print(' ')
winner = False #win checker
movecount = 0 #counts amount of turns
playgame = True
print ('welcome to Noughts & Crosses') #title
while playgame == True:
position = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] #sets the board spaces blank
plyr1list = []
plyr2list = []
gamelist = []
winner = False
movecount = 0
print (' ')
#Name input
print ('What is player 1s name?')
name1 = input()
print ('thanks '+ (name1) +', Whats player 2s name?')
name2 = input()
print ('so '+ (name1) +' is X and '+ (name2) + ' is O')
input("Press Enter to continue...")
printboard()
while (movecount < 9) and (winner == False):
if (movecount % 2 == 0): #player 1 turn
print ((name1) + 's ( X ) turn, please choose placement (1-9)')
move = input()
if move in ('1', '2', '3', '4', '5', '6', '7', '8', '9') and (int(move) not in (gamelist)):
plyr1list.append(int(move))
gamelist.append(int(move))
print (gamelist) #debug
position[int(move)-1] = ('X')
printboard()
movecount = movecount + 1
plyr1wintest()
elif move not in ('1', '2', '3', '4', '5', '6', '7', '8', '9'):
print ('That is not a valid move! Try again')
else: print ('That move is taken!, Try again')
else: #player 2 turn
print ((name2) + 's ( O ) turn, please choose placement (1-9)')
Thought it would be interesting seeing someone beginner code, vs someone's professional code on the same project. After I sent him my code he replied with how he would have done it, as a comparison.
**My code:**
import random
def plyr1wintest(): #checks if player has 3 in a row
global winner
if (1 in plyr1list) and (2 in plyr1list) and (3 in plyr1list) or \
(4 in plyr1list) and (5 in plyr1list) and (6 in plyr1list) or \
(7 in plyr1list) and (8 in plyr1list) and (9 in plyr1list) or \
(1 in plyr1list) and (4 in plyr1list) and (7 in plyr1list) or \
(2 in plyr1list) and (5 in plyr1list) and (8 in plyr1list) or \
(3 in plyr1list) and (6 in plyr1list) and (9 in plyr1list) or \
(1 in plyr1list) and (5 in plyr1list) and (9 in plyr1list) or \
(3 in plyr1list) and (5 in plyr1list) and (7 in plyr1list):
print ((name1) + ' wins as X')
winner = True
def plyr2wintest(): #checks if player has three in a row
global winner
if (1 in plyr2list) and (2 in plyr2list) and (3 in plyr2list) or \
(4 in plyr2list) and (5 in plyr2list) and (6 in plyr2list) or \
(7 in plyr2list) and (8 in plyr2list) and (9 in plyr2list) or \
(1 in plyr2list) and (4 in plyr2list) and (7 in plyr2list) or \
(2 in plyr2list) and (5 in plyr2list) and (8 in plyr2list) or \
(3 in plyr2list) and (6 in plyr2list) and (9 in plyr2list) or \
(1 in plyr2list) and (5 in plyr2list) and (9 in plyr2list) or \
(3 in plyr2list) and (5 in plyr2list) and (7 in plyr2list):
print ((name2) + ' wins as O')
winner = True
def printboard(): #print board
print(' ')
print(' '+ position[0] +' | '+ position[1] +' | '+ position[2] + ' ' + ' '+ '1' +' | '+ '2' +' | '+ '3')
print('-----------' + ' ' + '-----------')
print(' '+ position[3] +' | '+ position[4] +' | '+ position[5] + ' ' + ' '+ '4' +' | '+ '5' +' | '+ '6')
print('-----------' + ' ' + '-----------')
print(' '+ position[6] +' | '+ position[7] +' | '+ position[8] + ' ' + ' '+ '7' +' | '+ '8' +' | '+ '9')
print(' ')
winner = False #win checker
movecount = 0 #counts amount of turns
playgame = True
print ('welcome to Noughts & Crosses') #title
while playgame == True:
position = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] #sets the board spaces blank
plyr1list = []
plyr2list = []
gamelist = []
winner = False
movecount = 0
print (' ')
#Name input
print ('What is player 1s name?')
name1 = input()
print ('thanks '+ (name1) +', Whats player 2s name?')
name2 = input()
print ('so '+ (name1) +' is X and '+ (name2) + ' is O')
input("Press Enter to continue...")
printboard()
while (movecount < 9) and (winner == False):
if (movecount % 2 == 0): #player 1 turn
print ((name1) + 's ( X ) turn, please choose placement (1-9)')
move = input()
if move in ('1', '2', '3', '4', '5', '6', '7', '8', '9') and (int(move) not in (gamelist)):
plyr1list.append(int(move))
gamelist.append(int(move))
print (gamelist) #debug
position[int(move)-1] = ('X')
printboard()
movecount = movecount + 1
plyr1wintest()
elif move not in ('1', '2', '3', '4', '5', '6', '7', '8', '9'):
print ('That is not a valid move! Try again')
else: print ('That move is taken!, Try again')
else: #player 2 turn
print ((name2) + 's ( O ) turn, please choose placement (1-9)')
Updating Figure Display in Jupyter
Hello /r/IPython
​
I'm not sure if this is the right place to ask this, but the jupyter subreddit doesn't seem very active. Anywho, I'm trying grabbing images from webcam and doing some processing on it, and I want the output from the code block to update, maybe redrawing the figure/axis so that the newer image shows. Here's what I have:
```
plt.ion()
webcam = init_cam() #sets resolution and stuff
fig, ax = plt.subplots(1)
axim = ax.imshow(tFrame) #Show some pre-grabbed image
while True:
frame = get(webcam) #gets image from webcam
axim.set_data(frame)
plt.show(block=False)
#plt.pause(1) #I've tried with and without pauses
```
And long story short, it doesn't update the figure and appears to only show the first frame. Am I doing something wrong? Is there another way to do this? Any help would be appreciated!
/r/IPython
https://redd.it/k8a6nx
Hello /r/IPython
​
I'm not sure if this is the right place to ask this, but the jupyter subreddit doesn't seem very active. Anywho, I'm trying grabbing images from webcam and doing some processing on it, and I want the output from the code block to update, maybe redrawing the figure/axis so that the newer image shows. Here's what I have:
```
plt.ion()
webcam = init_cam() #sets resolution and stuff
fig, ax = plt.subplots(1)
axim = ax.imshow(tFrame) #Show some pre-grabbed image
while True:
frame = get(webcam) #gets image from webcam
axim.set_data(frame)
plt.show(block=False)
#plt.pause(1) #I've tried with and without pauses
```
And long story short, it doesn't update the figure and appears to only show the first frame. Am I doing something wrong? Is there another way to do this? Any help would be appreciated!
/r/IPython
https://redd.it/k8a6nx
reddit
Updating Figure Display in Jupyter
Hello /r/IPython I'm not sure if this is the right place to ask this, but the jupyter subreddit doesn't seem very active. Anywho, I'm...
Basic Power Analysis Discrepancy
Hi all,
I'm working on a power analysis to better understand how the process works for linear regression and interactions effects. I'm trying to create a function that simulates a dataset, adds participants to it based on an argument that can be specified (e.g., to see how many more people one would need to have power reach a certain threshold), and then counts a proportion of p-values less than an alpha level. In this case, the model is
dv ~ dx_status + ybocs + dx_status*ybocs
and I'm interested in learning how many participants I'd need to get a statistically significant p-value for the interaction term.
Here is the code:
import pandas as pd
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
hyp2_pvalues_list = [] #create an empty list
np.random.seed(4) #sets a seed for the random number generator
def pwrcurve_hypoth2(addtogroup = 0, simulations = 1000, es = 0.5, dv_sd = 3.9, bdi_sd = 5, ybocs_sd = 5,
alpha = .05):
for x in range(simulations):
/r/pystats
https://redd.it/lumrgk
Hi all,
I'm working on a power analysis to better understand how the process works for linear regression and interactions effects. I'm trying to create a function that simulates a dataset, adds participants to it based on an argument that can be specified (e.g., to see how many more people one would need to have power reach a certain threshold), and then counts a proportion of p-values less than an alpha level. In this case, the model is
dv ~ dx_status + ybocs + dx_status*ybocs
and I'm interested in learning how many participants I'd need to get a statistically significant p-value for the interaction term.
Here is the code:
import pandas as pd
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
hyp2_pvalues_list = [] #create an empty list
np.random.seed(4) #sets a seed for the random number generator
def pwrcurve_hypoth2(addtogroup = 0, simulations = 1000, es = 0.5, dv_sd = 3.9, bdi_sd = 5, ybocs_sd = 5,
alpha = .05):
for x in range(simulations):
/r/pystats
https://redd.it/lumrgk
reddit
Basic Power Analysis Discrepancy
Hi all, I'm working on a power analysis to better understand how the process works for linear regression and interactions effects. I'm trying to...