Data types and variables
Data within a program can be:
In Python you don't have to declare which data types you are using but you do have to understand the difference because programs will crash with a TYPE ERROR if you try to do something that is impossible - such as adding a string to an integer. VARIABLES Data is stored in containers known as VARIABLES. Data is stored so it can be used later in the program. The programmer creates a VARIABLE to hold one single item of data. There are RULES for naming VARIABLES. They must:
|
Simple dice game
In this game we will create a simple dice game where the dice is rolled twice and the highest score wins.
We will use ABSTRACTION because we will focus only on the numbers generated and will not think about what the dice look like. Open ThonnyYou will find Thonny inside the Computer Science Apps folder on the Desktop.
1. Save your file and name it simple dice game 2. Copy out the code shown in the images below. Include the comments |
Test your code after this first section. Use the Run icon or press F5 on the keyboard. Testing often makes DEBUGGING your code easier.
SYNTAX errors are the most common errors. They occur when the code is written wrong. A spelling error, or missing speech marks or brackets or a capital letter in the wrong place.
Leaving a blank line separates chunks of code to make it easier to read and understand. This is GOOD PROGRAMMING PRACTICE.
Using comments helps you and others understand your code.
Comments are GOOD PROGRAMMING PRACTICE.
Email me your finished dice game!
Extend the dice game
Take in the player's names. Player 1 and Player 2. You will need 2 variables to stored the names. An example is shown below.
Think about where you will put the code.
The SEQUENCE of code can be important in a program.
Think about where you will put the code.
The SEQUENCE of code can be important in a program.
Use the player's names in the program. For example, when they win. An example below.
Change the code so that the winner's name is displayed.
What else could we do to make the game better?
Write your ideas as # comments at the end of the code.
Write your ideas as # comments at the end of the code.
Email your teacher your extended dice game!
Python Syntax
Syntax is the set of rules that define how to write code in a programming language.
It is like the grammar of a human language, but for computers. If we make a mistake in the syntax like missing off a bracket or speech marks, the computer will not be able to understand the program and you will see a SYNTAX ERROR Programming involves logical thinking and applying the syntax of the Python language correctly. You will now practice simple tasks so you can focus on the syntax rather than on the problem solving. Click on the CS Newbs link here -----> |
Checkpoint
Programming Check - applying the skills you have learnt
Guess the number - the computer generates a random number between 1 and 10. The player guesses the number. Are they right?
- Using your dice game to help you, follow the flowchart to create the game.
Useful code:'is equal to' in Python' is 2 equal signs: ==
'is not equal to' is an exclamation mark and an equals sign: != - To convert from a string to an integer use int()...... just like you used str() in the dice game.
Email your program to your teacher.
Python syntax game
Check your Python syntax knowledge to crack the password and complete the game.