Mrs Stones Lessons
  • home
  • Year 7
    • Term 1
    • Term 2
    • Term 3
  • Year 8
    • Term 1 - HTML
    • Term 2
    • Term 3 >
      • Spreadsheets
      • Databases
      • Programming in Python
  • Edexcel ICT GCSE
  • GCSE Computing
  • Business Studies
    • Revision Help
Learning Intentions
TBAT - write clear readable code

Success Criteria
write the start of a short game called
                  MyMagic8Ball
Keywords
interactive mode
script mode
commenting
function
input
Starter - in your Computing folder create a sub-folder and name it Python, you must save all your python code in here. When you have done this click on the button and complete the quiz.
Quiz
Additional information -

The # symbol is not actually used by the program. It is there to help you and others understand the code. This is called
"commenting".

In the code below we have introduce a new word "import" this is followed by a Python module "random".
A module is special code that you can use, it brings a range of functions to your program.

It is very important to write code so that it is easy for you and others to understand it, long after you have written it as you may forget how you did things.

Python code is simpler than other computer languages, each line of code just needs to have a line ending. To group lines of code together you indent the code using four taps on the space bar. Idle will usually know when you should indent and do it for you when you press the return key. Be careful incorrect indenting like using 3 taps instead of four will cause errors.


# MyMagicBall

import random
 # these are the answers the user will be given
ans1="Go for it"
ans2="No way, Jose"
ans3="I'm not sure, Ask me again."
ans4="Fear of the unknown is what imprisons us."
ans5="It would be madness to do that"
ans6="Only you can save mankind"
ans7="Makes no difference to me, do or don't - whatever."
ans8="Yes, I think on balance that is the right choice."


Now open Python and type the code above into the script mode window. Save it into your python progam folder and name it "myMagicBall.py"

After you have saved your variables miss a line and type in the next part of the code below
Picture
The variable question has been given the value from the result of the  input() fuction. This means it now becomes (stores) whatever was typed by the person playing the game before they pressed the return.
In this next part of the code we are going to use the randint() function. You have to put random before randint() to tell the computer that this function is found in the random module.
Picture
The randint() function generates a random number between 1 and 8 and assigns the result to the variable called choice.
Look at the if, elif and else, what do you think this code is doing?
What about the compaison operator ==?
So if choice equals 1 what will the answer to the question be?
Powered by Create your own unique website with customizable templates.