In this ICS3U lesson you will be learning how to
Recall from the previous problem solving lesson, in ICS3U we solve a lot of problems in computer science using an algorithm. (A series of steps that lead you to the correct answer) The INPUT – PROCESSING – OUTPUT model of problem solving generally works pretty well in this course for solving straightforward computational problems
We are going to program a variation of the game of NIM between a human player and the computer player
Design the code that will generate the random coins that start the game and test to ensure it works correctly.
Processing:
Code:
import random
#Generate # of random coins to start
numCoins = random.randint(20,50)
print("There are", numCoins, "coins in this game")
Design the code so that the player should be able to continuously choose ANY number of coins (deal with limitations later) and the game will reduce the coins in the pile after each choice. It should stop when there are no more coins left.
Processing:
Create an infinite loop
Code:
#Infinite Game loop
while True:
#Player Choose the number of coins
pChoice = int(input("Enter the number of coins to pick: "))
#Reduce the number of coins
numCoins = numCoins - pChoice
#Check if the game should continue or the player loses
if numCoins > 0:
print("There are", numCoins, "coins left in this game")
else:
print("You Took the last coin, you lose")
break
Design the code so that the player can only choose 1, 2, or 3 coins. The game can’t stop if they choose incorrectly, it needs to keep asking until a valid input is chosen.
Processing:
Inside the infinite game loop:
Add another Infinite Loop
Code:
#Player Choose the number of coins & Check for valid input
while True:
pChoice = int(input("Enter the number of coins to pick: "))
#Check for invalid, if valid break out of choice loop
if pChoice > 3 or pChoice < 1 or numCoins - pChoice < 0:
print("Not Valid->Choose again")
else:
break
Design the code so that the computer will pick an appropriate amount of coins. It should have some limited intelligence and try to win if it has an opportunity to win.
Processing:
if numCoins > 3
if numCoins == 3
if numCoins == 2 or 1
After choosing the correct amount of coins:
Code:
#Computer choice based on # of coins remaining
if numCoins > 3:
cChoice = random.randint(1,3)
elif numCoins == 3:
cChoice = 2
elif numCoins == 2 or numCoins == 1:
cChoice = 1
print("Computer takes",cChoice,"coins")
#Reduce the number of coins
numCoins = numCoins - cChoice
#Check if the game should continue or the Computer loses
if numCoins > 0:
print("There are", numCoins, "coins left in this game")
else:
print("Computer Took the last coin, you win")
break
When it comes time to evaluate if this solution is correct, you have to do things a bit differently than with questions that have a lot of math. There is no math to verify in this program, so you need to check MANY different inputs to see if the program is behaving correctly for all of the different scenarios.
Here is the completed code for the game. Type it into the Python Editor and run the program
import random
#Generate # of random coins to start
numCoins = random.randint(20,50)
print("There are", numCoins, "coins in this game")
#Infinite Game loop
while True:
#Player Choose the number of coins & Check for valid input
while True:
pChoice = int(input("Enter the number of coins to pick: "))
#Check for invalid, if valid break out of choice loop
if pChoice > 3 or pChoice < 1 or numCoins - pChoice < 0:
print("Not Valid->Choose again")
else:
break
#Reduce the number of coins
numCoins = numCoins - pChoice
#Check if the game should continue or the player loses
if numCoins > 0:
print("There are", numCoins, "coins left in this game")
else:
print("You Took the last coin, you lose")
break
#Computer choice based on # of coins remaining
if numCoins > 3:
cChoice = random.randint(1,3)
elif numCoins == 3:
cChoice = 2
elif numCoins == 2 or numCoins == 1:
cChoice = 1
print("Computer takes",cChoice,"coins")
#Reduce the number of coins
numCoins = numCoins - cChoice
#Check if the game should continue or the Computer loses
if numCoins > 0:
print("There are", numCoins, "coins left in this game")
else:
print("Computer Took the last coin, you win")
break
I suggest you check if all the following scenarios are working correctly.
Try to code the following ICS3U questions using an IDE of your choice (Spyder, IDLE, Pycharm, etc). You will save those files on your computer for future reference.
Make sure you are completing the Input-Processing-Output Problem Solving Process before you start your code. Clearly evaluate the solution when finished.
Each question has:
Try your best to solve the problems yourself without looking at the solutions.
Make sure you test your programs with multiple different inputs to ensure it is functioning properly.
Name Your File : “ICS3UtargetSum.py”
You have two dice. An n sided dice and an m sided dice. n and m are inputted by the user. Your goal is to count and display all the possible combinations of dice rolls that will add up to a target number. That target number is entered by the user.
Here are some samples
#Required input
m = int(input("m:"))
n = int(input("n:"))
target = int(input("target Sum:"))
print("")
#counter
count = 0
#Loop through each value of the first dice
dice1 = 1
while dice1 <= m:
#Loop through each value of the second dice
dice2 = 1
while dice2 <= n:
#Total of both dice rolls
total = dice1 + dice2
#Check for target and increase count
if total == target:
print(dice1,"+",dice2,"=",total)
count = count + 1
#Move to the next dice2 value
dice2 = dice2 + 1
#Move to the next dice1 value
dice1 = dice1 + 1
if count == 1:
print("There is 1 way to get the sum of", target)
else:
print("There are",count,"ways to get the sum of",target)
Name Your File : “ICS3UguessingGame.py”
You are going to create a guessing game where the computer picks a random number between 1 and 100 and lets you guess the answer
Your program should also tell the user the following depending on the how far away they were from the actual answer
import random
target = random.randint(0,100)
while True:
#Get User Guess
guess = int(input("Guess a number: "))
distance = guess - target
#Check hint1
if distance > 0:
hint1 = "Too High"
elif distance < 0:
hint1 = "Too Low"
distance = distance *-1
else:
break
#Check hint2
if distance > 0 and distance <=5 :
hint2 = "Boiling"
elif distance > 5 and distance <=10 :
hint2 = "Hot"
elif distance > 10 and distance <=20 :
hint2 = "Warm"
elif distance > 20 and distance <= 50 :
hint2 = "Cold"
else:
hint2 = "Freezing"
#Display results
print(hint1,",",hint2)
print("You got it")
Name Your File : “ICS3UsumacSequence.py”
A sumac sequence is made by beginning with two integer terms (the first larger than the second), and then producing more terms according to the following rules (starting with the third term)
Here are some examples
a = int(input("First: "))
b = int(input("Second: "))
#Start of the sequence
print("\nThe sequence is: ",end ="")
print(a,b, end = " ")
while True:
#Find the next number in the sequence and print it
c = a - b
print(c, end =" ")
#Check if sequence should end, if not then reset the first and second number
if c > b:
break
else:
a = b
b = c
Name Your File : “ICS3UsnakesLadders.py”
You want to simulate a “snakes and ladders” game using the board shown below. Assume its only 1 player playing. You are using 2 six sided dice. To win, you must land on 100 exactly. If your roll would put you over 100, then you don’t move. Indicate to the player if they land on a snake or a ladder and move to the appropriate spot. The player always starts on space 1
Remember that snakes move you down and ladders move you up
import random
space = 1
print("You are on space 1")
while True:
#Roll the dice
input("Press Enter to Roll")
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print("You rolled:",dice1, dice2)
#Update location and check for snakes / ladders, win
tempSpace = space + dice1 + dice2
if tempSpace == 9:
space = 34
print("Found a ladder")
elif tempSpace == 40:
space = 64
print("Found a ladder")
elif tempSpace == 67:
space = 86
print("Found a ladder")
elif tempSpace == 54:
space = 19
print("Found a snake")
elif tempSpace == 90:
space = 48
print("Found a snake")
elif tempSpace == 99:
space = 77
print("Found a snake" )
elif tempSpace > 100:
print("Too many, can't move")
elif tempSpace == 100:
break
else:
space = tempSpace
print("You are now on space", space)
#Game Over
print("Space 100...You Win")