Skip to content

Commit fa2dcc1

Browse files
authored
Add files via upload
1 parent e57d1b4 commit fa2dcc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2591
-0
lines changed

Shivaansh.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from __future__ import print_function
2+
3+
x = input("Enter a number: ")
4+
for i in range(1, 11, 1):
5+
print(x, "x", i, "=", (x * i))

SimpleCalculator.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def add(a, b):
2+
return a + b
3+
4+
5+
def subtract(a, b):
6+
return a - b
7+
8+
9+
def multiply(a, b):
10+
return a * b
11+
12+
13+
def divide(a, b):
14+
try:
15+
return a / b
16+
except ZeroDivisionError:
17+
return "Zero Division Error"
18+
19+
def main():
20+
print("Select Operation")
21+
print("1.Add")
22+
print("2.Subtract")
23+
print("3.Multiply")
24+
print("4.Divide")
25+
26+
choice = input("Enter Choice(+,-,*,/): ")
27+
num1 = int(input("Enter first number: "))
28+
num2 = int(input("Enbter Second number:"))
29+
30+
if choice == '+':
31+
print(num1, "+", num2, "=", add(num1, num2))
32+
33+
elif choice == '-':
34+
print(num1, "-", num2, "=", subtract(num1, num2))
35+
36+
elif choice == '*':
37+
print(num1, "*", num2, "=", multiply(num1, num2))
38+
39+
elif choice == '/':
40+
print(num1, "/", num2, "=", divide(num1, num2))
41+
else:
42+
print("Invalid input")
43+
main()
44+
main()

SimpleStopWatch.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Author: OMKAR PATHAK
2+
# This script helps to build a simple stopwatch application using Python's time module.
3+
4+
import time
5+
6+
print('Press ENTER to begin, Press Ctrl + C to stop')
7+
while True:
8+
try:
9+
input() # For ENTER. Use raw_input() if you are running python 2.x instead of input()
10+
starttime = time.time()
11+
print('Started')
12+
while True:
13+
print('Time Elapsed: ', round(time.time() - starttime, 0), 'secs', end="\r")
14+
time.sleep(1) # 1 second delay
15+
except KeyboardInterrupt:
16+
print('Stopped')
17+
endtime = time.time()
18+
print('Total Time:', round(endtime - starttime, 2), 'secs')
19+
break
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import json
2+
import time
3+
4+
import MySQLdb
5+
from tweepy import OAuthHandler
6+
from tweepy import Stream
7+
from tweepy.streaming import StreamListener
8+
9+
# replace mysql.server with "localhost" if you are running via your own server!
10+
# server MySQL username MySQL pass Database name.
11+
conn = MySQLdb.connect("mysql.server", "beginneraccount", "cookies", "beginneraccount$tutorial")
12+
13+
c = conn.cursor()
14+
15+
# consumer key, consumer secret, access token, access secret.
16+
ckey = "asdfsafsafsaf"
17+
csecret = "asdfasdfsadfsa"
18+
atoken = "asdfsadfsafsaf-asdfsaf"
19+
asecret = "asdfsadfsadfsadfsadfsad"
20+
21+
22+
class listener(StreamListener):
23+
24+
def on_data(self, data):
25+
all_data = json.loads(data)
26+
27+
tweet = all_data["text"]
28+
29+
username = all_data["user"]["screen_name"]
30+
31+
c.execute("INSERT INTO taula (time, username, tweet) VALUES (%s,%s,%s)",
32+
(time.time(), username, tweet))
33+
34+
conn.commit()
35+
36+
print((username, tweet))
37+
38+
return True
39+
40+
def on_error(self, status):
41+
print(status)
42+
43+
44+
auth = OAuthHandler(ckey, csecret)
45+
auth.set_access_token(atoken, asecret)
46+
47+
twitterStream = Stream(auth, listener())
48+
twitterStream.filter(track=["car"])

TTS.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from tkinter import *
2+
from platform import system
3+
if system() == 'Windows' or 'nt':
4+
import win32com.client as wincl
5+
else:
6+
print("Sorry, TTS client is not supported on Linux or MacOS")
7+
exit()
8+
9+
def text2Speech():
10+
text = e.get()
11+
speak = wincl.Dispatch("SAPI.SpVoice")
12+
speak.Speak(text)
13+
14+
15+
# window configs
16+
tts = Tk()
17+
tts.wm_title("Text to Speech")
18+
tts.geometry("225x105")
19+
tts.config(background="#708090")
20+
21+
f = Frame(tts, height=280, width=500, bg="#bebebe")
22+
f.grid(row=0, column=0, padx=10, pady=5)
23+
lbl = Label(f, text="Enter your Text here : ")
24+
lbl.grid(row=1, column=0, padx=10, pady=2)
25+
e = Entry(f, width=30)
26+
e.grid(row=2, column=0, padx=10, pady=2)
27+
btn = Button(f, text="Speak", command=text2Speech)
28+
btn.grid(row=3, column=0, padx=20, pady=10)
29+
tts.mainloop()

TicTacToe.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# Tic Tac Toe
2+
3+
import random
4+
5+
try:
6+
input = raw_input
7+
except NameError:
8+
pass
9+
10+
11+
def drawBoard(board):
12+
# This function prints out the board that it was passed.
13+
14+
# "board" is a list of 10 strings representing the board (ignore index 0)
15+
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
16+
print('-----------')
17+
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
18+
print('-----------')
19+
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
20+
21+
22+
def inputPlayerLetter():
23+
# Lets the player type which letter they want to be.
24+
# Returns a list with the player's letter as the first item, and the computer's letter as the second.
25+
letter = ''
26+
while letter not in ('X', 'O'):
27+
letter = input('Do you want to be X or O? ').upper()
28+
29+
# the first element in the tuple is the player's letter, the second is the computer's letter.
30+
31+
return ['X', 'O'] if letter == 'X' else ['O', 'X']
32+
33+
34+
def whoGoesFirst():
35+
# Randomly choose the player who goes first.
36+
return random.choice(('computer', 'player'))
37+
38+
39+
def playAgain():
40+
# This function returns True if the player wants to play again, otherwise it returns False.
41+
print('Do you want to play again? (yes or no)')
42+
return input().lower().startswith('y')
43+
44+
45+
def makeMove(board, letter, move):
46+
if isSpaceFree(board, move):
47+
board[move] = letter
48+
else:
49+
raise Exception("makeMove: the field is not empty!")
50+
51+
52+
def isWinner(bo, le):
53+
# Given a board and a player's letter, this function returns True if that player has won.
54+
# We use bo instead of board and le instead of letter so we don't have to type as much.
55+
return ((bo[7] == le and bo[8] == le and bo[9] == le) or # across the top
56+
(bo[4] == le and bo[5] == le and bo[6] == le) or # across the middle
57+
(bo[1] == le and bo[2] == le and bo[3] == le) or # across the bottom
58+
(bo[7] == le and bo[4] == le and bo[1] == le) or # down the left side
59+
(bo[8] == le and bo[5] == le and bo[2] == le) or # down the middle
60+
(bo[9] == le and bo[6] == le and bo[3] == le) or # down the right side
61+
(bo[7] == le and bo[5] == le and bo[3] == le) or # diagonal
62+
(bo[9] == le and bo[5] == le and bo[1] == le)) # diagonal
63+
64+
65+
def getBoardCopy(board):
66+
# Make a duplicate of the board list and return it the duplicate.
67+
dupeBoard = []
68+
69+
for i in board:
70+
dupeBoard.append(i)
71+
72+
return dupeBoard
73+
74+
75+
def isSpaceFree(board, move):
76+
# Return true if the passed move is free on the passed board.
77+
return board[move].isdigit()
78+
79+
80+
def getPlayerMove(board):
81+
# Let the player type in his move.
82+
move = ' '
83+
while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
84+
print('What is your next move? (1-9)')
85+
move = input()
86+
return int(move)
87+
88+
89+
def chooseRandomMoveFromList(board, movesList):
90+
# Returns a valid move from the passed list on the passed board.
91+
# Returns None if there is no valid move.
92+
possibleMoves = [i for i in movesList if isSpaceFree(board, i)]
93+
return random.choice(possibleMoves) if possibleMoves else None
94+
95+
96+
def getComputerMove(board, computerLetter):
97+
# Given a board and the computer's letter, determine where to move and return that move.
98+
playerLetter = 'O' if computerLetter == 'X' else 'X'
99+
100+
# Here is our algorithm for our Tic Tac Toe AI:
101+
# First, check if we can win in the next move
102+
for i in range(1, 10):
103+
copy = getBoardCopy(board)
104+
if isSpaceFree(copy, i):
105+
makeMove(copy, computerLetter, i)
106+
if isWinner(copy, computerLetter):
107+
return i
108+
109+
# Check if the player could win on his next move, and block them.
110+
for i in range(1, 10):
111+
copy = getBoardCopy(board)
112+
if isSpaceFree(copy, i):
113+
makeMove(copy, playerLetter, i)
114+
if isWinner(copy, playerLetter):
115+
return i
116+
117+
# Try to take one of the corners, if they are free.
118+
move = chooseRandomMoveFromList(board, [1, 3, 7, 9])
119+
if move != None:
120+
return move
121+
122+
# Try to take the center, if it is free.
123+
if isSpaceFree(board, 5):
124+
return 5
125+
126+
# Move on one of the sides.
127+
return chooseRandomMoveFromList(board, [2, 4, 6, 8])
128+
129+
130+
def isBoardFull(board):
131+
# Return True if every space on the board has been taken. Otherwise return False.
132+
for i in range(1, 10):
133+
if isSpaceFree(board, i):
134+
return False
135+
return True
136+
137+
138+
def main():
139+
print('Welcome to Tic Tac Toe!')
140+
random.seed()
141+
while True:
142+
# Reset the board
143+
theBoard = [' '] * 10
144+
for i in range(9, 0, -1):
145+
theBoard[i] = str(i)
146+
playerLetter, computerLetter = inputPlayerLetter()
147+
turn = whoGoesFirst()
148+
print('The ' + turn + ' will go first.')
149+
gameIsPlaying = True
150+
151+
while gameIsPlaying:
152+
if turn == 'player':
153+
# Player's turn.
154+
drawBoard(theBoard)
155+
move = getPlayerMove(theBoard)
156+
makeMove(theBoard, playerLetter, move)
157+
158+
if isWinner(theBoard, playerLetter):
159+
drawBoard(theBoard)
160+
print('Hooray! You have won the game!')
161+
gameIsPlaying = False
162+
else:
163+
if isBoardFull(theBoard):
164+
drawBoard(theBoard)
165+
print('The game is a tie!')
166+
break
167+
else:
168+
turn = 'computer'
169+
170+
else:
171+
# Computer's turn.
172+
move = getComputerMove(theBoard, computerLetter)
173+
makeMove(theBoard, computerLetter, move)
174+
175+
if isWinner(theBoard, computerLetter):
176+
drawBoard(theBoard)
177+
print('The computer has beaten you! You lose.')
178+
gameIsPlaying = False
179+
else:
180+
if isBoardFull(theBoard):
181+
drawBoard(theBoard)
182+
print('The game is a tie!')
183+
break
184+
else:
185+
turn = 'player'
186+
187+
if not playAgain():
188+
break
189+
190+
191+
if __name__ == "__main__":
192+
main()

0 commit comments

Comments
 (0)