Upload
chloe-turner
View
220
Download
0
Embed Size (px)
DESCRIPTION
Code analysis [ slide 3 ] # The is_same() function def is_same(target, number): if target == number: result="Win" elif target > number: result="Low" else: result="High" return result # Collect the user's guess as an integer guess = int(input("Can you guess it? ")) # Use our function higher_or_lower = is_same(computer_number, guess) # Run the game until the user is correct while higher_or_lower != "Win": if higher_or_lower == "Low": guess = int(input("Sorry, you are too low. Try again. ")) else: guess = int(input("Sorry, you are too high. Try again. ")) higher_or_lower = is_same(computer_number, guess) # End the game input("Correct! Well Done Press ENTER to exit.")
Citation preview
Coding TimeThis is a starter activity and should take about 10 minutes
[ slide 1 ]
1. Log in to your computer2. Open IDLE3. Start a script session (Select New Window from the File
menu.)4. Copy the code from Code Box 4.4 on page 51 and 52 into
your file.5. Save your file as myNumber.pyTop Tip: Think about what the code is doing as you type.
There are only two bits of code you will not recognise:return and int()
If you have finished:Check your code carefully and then run it:( Run Module from the Run menu )
Lesson Aims[ slide 2 ]
Vocabularydata-typescasting
In this lesson you are going to:
1. Learn more about writing your own functions2. Learn about the return keyword3. Learn about casting
Code analysis[ slide 3 ]
# The is_same() functiondef is_same(target, number): if target == number: result="Win" elif target > number: result="Low" else: result="High" return result
# Collect the user's guess as an integerguess = int(input("Can you guess it? "))
# Use our functionhigher_or_lower = is_same(computer_number, guess)
# Run the game until the user is correctwhile higher_or_lower != "Win": if higher_or_lower == "Low": guess = int(input("Sorry, you are too low. Try again. ")) else: guess = int(input("Sorry, you are too high. Try again. ")) higher_or_lower = is_same(computer_number, guess)
# End the gameinput("Correct!\nWell Done\n\n\nPress ENTER to exit.")
Taking it further[ slide 4 ]
Work through the Ideas from page 54
Optional Homework
Try any of the Ideas from page 54
[ slide 5 ]
Lesson Summary[ slide 6 ]
In this lesson you have:1. Learnt more about writing your own functions2. Learnt about the return keyword3. Learnt about casting
Vocabulary1. What is the keyword we use if we want a function to produce a value?
3. What word describes the process in question 2?
2. What function do we use if we want to change a number which is a string data-type into an integer? int()
casting
return