Lesson 22 Feb
Step 2 Application Programming Activities
Input Output
Independent work: Coding challenge
1: Write a program that asks the user to enter how many children are in their class and how many exercise books each child has. The program should then output how many exercise books there are altogether.
Coding challenge 2: Write a program which asks the user their name and how many days until their next birthday. The program should then output a personalised sentence with a rough approximation of the number of seconds until their next birthday.
Loop Intro
*** Write a program that asks the user for a number n and prints the sum of the numbers 1 to n
Step 3 Guessing Game with While Loop
In this program you will use the random number generator to create a number in a specified range. The program will let the user guess the number until the user guesses the correct number or enters a negative number to stop the game.
Extension for full marks : extend the game to enable the user to enter the upper and lower values of the range.
Guessing Game Starting Code
import random
upper_bound = 20
lower_bound = 1
#to_be_guessed = int(n * random.random()) + 1
to_be_guessed = random.randint(lower_bound, upper_bound)
guess = 0
# add the while loopโฆโฆ.
Step 4 Conditional Satements - If Else Then
4.1.9 Learning Objectives ( thinking ahead )
Identify the inputs and outputs required in a solution.
Step 5 Programming Activity
The program below will always prompt for 3 numbers and add these together. You have been asked to extend the program below to first prompt the use for how many numbers they wish to add, if they answer 3 for example then prompt for 3 numbers and add them together and print out the result.!!
Adventure Game Mix It Up
You can copy and paste, but you will need to format / indent to get the program to execute.
At the momnet the adventure game has 2 caves. The challenge is to add 2 more caves and give the user a choice of 1 ,2,3 or 4 caves!
import random
import time
def displayIntro():
print('You are in a land full of dragons. In front of you,')
print('you see two caves. In one cave, the dragon is friendly')
print('and will share his treasure with you. The other dragon')
print('is greedy and hungry, and will eat you on sight.')
print()
def chooseCave():
cave = ''
while cave != '1' and cave != '2':
print('Which cave will you go into? (1 or 2)')
cave = input()
return cave
def checkCave(chosenCave):
print('You approach the cave...')
time.sleep(2)
print('It is dark and spooky...')
time.sleep(2)
print('A large dragon jumps out in front of you! He opens his jaws and...')
print()
time.sleep(2)
friendlyCave = random.randint(1, 2)
if chosenCave == str(friendlyCave):
print('Gives you his treasure!')
else:
print('Gobbles you down in one bite!')
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
displayIntro()
caveNumber = chooseCave()
checkCave(caveNumber)
print('Do you want to play again? (yes or no)')
playAgain = input()