1 print("Hello, World!
")
2
1 name = "John"
2 age = 30
3 print("My name is", name, "and I am", age, "years old.")
1 fruits = ["apple", "banana", "cherry", "date", "grape"]
2
3 for fruit in fruits:
4 print(fruit)
1 num1 = 10
2 num2 = 5
3
4 sum_result = num1 + num2
5 difference_result = num1 - num2
6 product_result = num1 * num2
7 quotient_result = num1 / num2
8
9 print("Sum:", sum_result)
10 print("Difference:", difference_result)
11 print("Product:", product_result)
12 print("Quotient:", quotient_result)
1 for i in range(1, 11):
2 print(i)
1 score = 75
2
3 if score >= 60 and score <= 100:
4 print("Passed")
5 else:
6 print("Failed")
1 number = 14
2
3 if number > 10 and number % 2 == 0:
4 print(f"{number} is greater than 10 and even.")
5 else:
6 print(f"{number} is not greater than 10 and even.")
1 password = "password123"
2 confirm_password = "password123"
3
4 if password == confirm_password:
5 print("Password successfully confirmed.")
6 else:
7 print("Error: Passwords do not match.")
1 for i in range(1, 11):
2 print(i)
total_cost
portion_down_payment
portion_down_payment = 0.25
current_savings current_savings
r
current_savings*r/12
r r = 0.04
annual_salary
portion_saved
monthly salary annual salary / 12
annual_salary
portion_saved
total_cost
Enter your annual salary: 120000
Enter the percent of your salary to save, as a decimal: .10
Enter the cost of your dream home: 1000000
Number of months: 183
Enter your annual salary: 80000
Enter the percent of your salary to save, as a decimal: .15
Enter the cost of your dream home: 500000
Number of months: 105
input()
string int
1 #data#
2 annual_salary = int(input("enter the annual salary:"))
3 portion_saved = float(input("enter the portion to save, as a decimal:"))
4 total_cost = float(input("enter the cost of your dream house:"))
5 r = 0.04
6 portion_down_payments = 0.25
7 down_payments = portion_down_payments * total_cost
8 current_savings = 0
9 number_of_months = 0
10
11 #calculation#
12 while current_savings <= down_payments:
13 current_savings += portion_saved*annual_salary/12 + current_savings*r/12
14 number_of_months += 1
15
16 print("it will take you",number_of_months ,"months to save enough money to make the down payment."
enter the annual salary:1000
enter the portion to save, as a decimal:0.1
enter the cost of your dream house:1000000
it will take you 1387 months to save enough money to make the down payment.
Main Algorithm 1
Variables
integer NbSticks
integer NbRemoved
integer cp
Begin
cp <- 0
NbSticks <- init_game()
while NbSticks>0 do
NbRemoved <- human_play(NbSticks)
NbSticks <- NbSticks - NbRemoved
cp <- cp + 1
endWhile
if NbSticks is an even then // if NbSticks mod 2 == 0
write("Player 1 win in ",cp," turns")
else
else
write("Player 2 win in ",cp," turns")
endif
End
Function n <- init_game()
Input
Output
integer n
Begin
n <- -1
while n<1 do
write("Enter the initial number of sticks (>0)")
n <- read()
endwhile
return n
End
Function r <- human_play(n)
Input
integer n
Output
integer r
Begin
r <- 0
while r<1 or r>min(3,n) do
write("How many sticks do you want to remove ?")
r <- read()
endwhile
return r
End
Function r <- computer_play(n)
Input
integer n
Output
integer r
Begin
r <- (n-1) mod 4
if r egual 0 then
r <- random(1,3)
endif
return r
End
Main Algorithm 2
Variables
integer NbSticks
integer NbRemoved
integer cp
integer p
vector of character PType(2)
Begin
cp <- 0
NbSticks,PType, <- init_game2()
while NbSticks>0 do
while NbSticks>0 do
p <- cp mod 2
if v[p] = 'h' then
NbRemoved <- human_play(NbSticks)
else
NbRemoved <- computer_play(NbSticks)
NbSticks <- NbSticks - NbRemoved
cp <- cp + 1
endWhile
if NbSticks is an even then // if NbSticks mod 2 == 0
write("Player 1 win in ",cp," turns")
else
write("Player 2 win in ",cp," turns")
endif
End
Function n,v <- init_game2()
Input
Output
integer n
vector of character v(2)
Begin
n <- -1
while n<1 do
write("Enter the initial number of sticks (>0)")
n <- read()
endwhile
for i <- 0 to 2 do
v[i] <- 'a'
while v[i] != 'c' or v[i] != 'h' do
write("Enter type of player ",i," ('h' or 'c')")
v[i] <- read()
endwhile
endfor
return n,v
End
1 print("Py Nim\n")
2
3 def getTokens(curTokens):
4 global tokens
5
6 print("How many tokens would you like to take? ", end='')
7 take = int(input())
8
9 if (take < 1 or take > 3):
10 print("Number must be between 1 and 3.\n")
11 getTokens(curTokens)
12 return
13
14 tokens = curTokens - take
15 print(f'You take {take} tokens.')
16 print(f'{tokens} tokens remaining.\n')
17
18 def compTurn(curTokens):
19 global tokens
20
21 take = curTokens %
21 take = curTokens % 4
22 tokens = curTokens - take
23 print (f'Computer takes {take} tokens.')
24 print (f'{tokens} tokens remaining.\n')
25
26
27 tokens = 12
28 while (tokens > 0):
29 getTokens(tokens)
30 compTurn(tokens)
31
32 print("Computer wins!")
Py Nim
How many tokens would you like to take? 3
You take 3 tokens.
9 tokens remaining.
Computer takes 1 tokens.
8 tokens remaining.
How many tokens would you like to take? 3
You take 3 tokens.
5 tokens remaining.
Computer takes 1 tokens.
4 tokens remaining.
How many tokens would you like to take? 1
You take 1 tokens.
3 tokens remaining.
Computer takes 3 tokens.
0 tokens remaining.
Computer wins!
1 #Implementation of Two Player Tic-Tac-Toe game in Python.
2
3 ''' We will make the board using dictionary
4 in which keys will be the location(i.e : top-left,mid-right,etc.)
5 and initialliy it's values will be empty space and then after every move
6 we will change the value according to player's choice of move. '''
7
8 theBoard = {'7': ' ' , '8': ' ' , '9': ' ' ,
9 '4': ' ' '5': ' ' '6': ' '
9 '4': ' ' , '5': ' ' , '6': ' ' ,
10 '1': ' ' , '2': ' ' , '3': ' ' }
11
12 board_keys = []
13
14 for key in theBoard:
15 board_keys.append(key)
16
17 ''' We will have to print the updated board after every move in the game and
18 thus we will make a function in which we'll define the printBoard function
19 so that we can easily print the board everytime by calling this function. '''
20
21 def printBoard(board):
22 print(board['7'] + '|' + board['8'] + '|' + board['9'])
23 print('-+-+-')
24 print(board['4'] + '|' + board['5'] + '|' + board['6'])
25 print('-+-+-')
26 print(board['1'] + '|' + board['2'] + '|' + board['3'])
27
28 # Now we'll write the main function which has all the gameplay functionality.
29 def game():
30
31 turn = 'X'
32 count = 0
33
34
35 for i in range(10):
36 printBoard(theBoard)
37 print("It's your turn," + turn + ".Move to which place?")
38
39 move = input()
40
41 if theBoard[move] == ' ':
42 theBoard[move] = turn
43 count += 1
44 else:
45 print("That place is already filled.\nMove to which place?")
46 continue
47
48 # Now we will check if player X or O has won,for every move after 5 moves.
49 if count >= 5:
50 if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the top
51 printBoard(theBoard)
52 print("\nGame Over.\n")
53 print(" **** " +turn + " won. ****")
54 break
55 elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle
56 printBoard(theBoard)
57 print("\nGame Over.\n")
58 print(" **** " +turn + " won. ****")
59 break
60 elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the bottom
61 printBoard(theBoard)
62 print("\nGame Over.\n")
63 print(" **** " +turn + " won. ****")
64 break
65 elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': # down the left side
66 printBoard(theBoard)
67 print("\nGame Over.\n")
68 print(" **** " +turn + " won. ****")
69 break
70 elif theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ': # down the middle
71 printBoard(theBoard)
72 print("\nGame Over.\n")
73 print(" **** " +turn + " won. ****")
74 break
74 break
75 elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': # down the right side
76 printBoard(theBoard)
77 print("\nGame Over.\n")
78 print(" **** " +turn + " won. ****")
79 break
80 elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal
81 printBoard(theBoard)
82 print("\nGame Over.\n")
83 print(" **** " +turn + " won. ****")
84 break
85 elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal
86 printBoard(theBoard)
87 print("\nGame Over.\n")
88 print(" **** " +turn + " won. ****")
89 break
90
91 # If neither X nor O wins and the board is full, we'll declare the result as 'tie'.
92 if count == 9:
93 print("\nGame Over.\n")
94 print("It's a Tie!!")
95
96 # Now we have to change the player after every move.
97 if turn =='X':
98 turn = 'O'
99 else:
100 turn = 'X'
101
102 # Now we will ask if player wants to restart the game or not.
103 restart = input("Do want to play Again?(y/n)")
104 if restart == "y" or restart == "Y":
105 for key in board_keys:
106 theBoard[key] = " "
107
108 game()
109
110 if __name__ == "__main__":
111 game()
112