INFO II Practice 1 With Solutions
INFO II Practice 1 With Solutions
")
2
1 name = "John"
2 age = 30
3 print("My name is", name, "and I am", age, "years old.")
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.")
r
current_savings*r/12
r r = 0.04
annual_salary
portion_saved
annual_salary
portion_saved
total_cost
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."
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
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
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
Computer wins!