Lesson 7
Show what you know - this lesson you will work independently to demonstrate the skills and knowledge you have acquired over the last lessons.
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.
You are going to write a program that will allow the player to ask for advice, will shake the magic ball and magically respond with an answer.
# 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
Show what you know - this lesson you will work independently to demonstrate the skills and knowledge you have acquired over the last lessons.
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.
You are going to write a program that will allow the player to ask for advice, will shake the magic ball and magically respond with an answer.
# 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
The randint() function generates a random number between 1 and 8 and assigns the result to the variable called choice.
Now copy the next part of the code. This gives your player a random answer to their question.
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.
Now copy the next part of the code. This gives your player a random answer to their question.
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.
Look at the if, elif and else, what do you think this code is doing?
What about the comparison operator ==?
So if choice equals 1 what will the answer to the question be?
Could you change the questions to make an even better Magic Ball game?
Teacher notes - download code document
What about the comparison operator ==?
So if choice equals 1 what will the answer to the question be?
Could you change the questions to make an even better Magic Ball game?
Teacher notes - download code document