1 / 49

Small Basic to Python CPD Training

Small Basic to Python CPD Training. CAS CPD. Schedule. 15:15 Refreshments 15:30 - 15:45 Introduction 15:45 - 16:45 Small Basic 16:45 - 18:00 Python Workshop 18:00 - 18:15 Scheme of Work 18:15 - 18:30 Networking. Introduction to Small Basic. Why Small Basic for student

whitelisa
Download Presentation

Small Basic to Python CPD Training

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Small Basic to Python CPD Training CAS CPD

  2. Schedule • 15:15 Refreshments • 15:30 - 15:45 Introduction • 15:45 - 16:45 Small Basic • 16:45 - 18:00 Python Workshop • 18:00 - 18:15 Scheme of Work • 18:15 - 18:30 Networking

  3. Introduction to Small Basic • Why Small Basic for student • One big reason is that it’s free from Microsoft • It is specifically designed for programmers who would be described as beginners, but with the benefits of a real coding environment. • It provide the gap between Scratch and Python • It builds confidence from which you can move onto other, more complex language, big brother Visual Basic • There are many built-in elements that make your work simpler and the language itself is very simple

  4. Introduction to Small Basic Intelligent Support Another advantage of Small Basic is the fact that it gives you support as you are entering in your code. So as you type, a suggestion is made as to which command or variable you are wanting to use and will fill in for you. It will also give an explanation of what the command does. This means that many of the syntax errors which cause confusion and frustration are avoided.

  5. Introduction to Small Basic Run the program • How to Open Small Basic • Start  All Program  Microsoft Small Basic Code Area Execution Mode Area

  6. Introduction to Small Basic • Your First Program (Printing) • You will open Small Basic and create a program that print “Hello World” • As you start typing in small basic will suggest the command for you TextWindow.WriteLine("Hello World”)

  7. INPUT in Small Basic • Extending to Input • We will now extend our program to as for user input • The program will print the user input • We use Read command to input • Try the example below TextWindow.WriteLine(“What is your name”) name = TextWindow.Read() TextWindow.Write("Hello " +name)

  8. Reviewin Artificial Intelligence from using Chatbots • To develop a better understanding of some current artificial intelligence technologies, look at some demonstrations below; • Hold down Ctrl and click on the link to access the online Chatbot • Jabberwacky: http://www.jabberwacky.com/ • Cleverbot: http://www.cleverbot.com/ • Elbot: http://www.elbot.com/

  9. Creating a Simple AI • Creating a Simple Artificial Intelligent (AI) • From the example below we can create a simple AI. • Extend the example below to create a simple AI • At this stage you should start encouraging students to start commenting on their code by using single or double quotation mark (“ or ‘) TextWindow.WriteLine(“What is your name”) name = TextWindow.Read() TextWindow.WriteLine("Hello " +name) TextWindow.WriteLine(“What is your favourite food?”) food= TextWindow.Read() TextWindow.WriteLine(“Oh " +food + “ is my favorite too" )

  10. Small Basic (IF Function) • Creating a Simple a simple quiz • From the example below we can create a simple quiz. TextWindow.WriteLine(“What is your name”) name = TextWindow.Read() TextWindow.WriteLine(“What is 2 + 2”) answer= TextWindow.Read() IfAnswer = "4" Then TextWindow.WriteLine ("Well done " + Name) Else TextWindow.WriteLine("That is the wrong answer" + Name + ", the answer was 4") EndIf

  11. 45 my_number variable name The variable’s value The box represents the space reserved in the computer’s memory What is a Variable A variable is a space in the computer’s memory where we can store data such as a number or text. The value can be changed if required. To create a variable and give it the value 45 we write: my_number=45

  12. Activity numberOne = 3 numberTwo = 4 answer = numberOne + numberTwo TextWindow.WriteLine(answer) • You can also perform calculations that use a variable • You can use a variable in a calculation, and use the same variable to store the answer. • Predict the following code: score = 112 score = score + 1 TextWindow.WriteLine(score)

  13. Small Basic (Adding Score Variable) • Creating a Simple a simple quiz • From the example below add a score to your quiz. score = 0 TextWindow.WriteLine(“What is 2 + 2”) answer= TextWindow.Read() IfAnswer = "4" Then TextWindow.WriteLine ("Well done " + Name) score = score + 1 Else TextWindow.WriteLine("That is the wrong answer" + Name + ", the answer was 4") EndIf TextWindow.WriteLine(“Your score is ” + score)

  14. EXTENDING THE QUIZ One thing that we may wish to do with the quiz would be to allow the user to take the quiz again depending on the feedback. To do this, we need to use a label and a gotostatement which directs the program to the label. The syntax for the label is labelname: and is positioned at any point in the programme where you would it to restart from. TextWindow.WriteLine("do you want to run again?") answer = textwindow.read() If answer = "y" Then Gotostartagain EndIf

  15. Adding Goto Goto statement allow you to start executing at a particular point in your code This is the cheating way of doing loop • Activity • Add a “StartAgain” at the to top of your quiz • Add the code below Answer1 = TextWindow.Read() If Answer1 = "y" Then Goto StartAgain EndIf

  16. Adding Responsive Feedback We are now going to add a responsive feedback to the quiz base on the score • Activity • Assuming that you have 5 questions in your quiz, • Add a IF function that does the following • IF the score is more than 4 THEN • Display “Excellent work player name” • ELSE IF score is less than 3 THEN • Display “Good Work” • Else: • Display you need more practice and try again

  17. Adding Responsive Feedback Solution If score = 5 Then TextWindow.WriteLine ("Excellent work " + Name + " Your score is: " + score) ElseIfscore > 3 Then TextWindow.WriteLine ("Good work " + Name + " Your score is: " + score) Else TextWindow.WriteLine ("Try again " + Name + " Your score is: " + score) TextWindow.WriteLine ("Do you want to try again?") Answer1 = TextWindow.Read() If Answer1 = "y" Then Goto StartAgain EndIf EndIf

  18. Intro to python Programming CPD

  19. Using the Interactive Mode with Python • Type in the following, exactly as it shown here and then press the return key. print(“Hello World”) • The phrase Hello World should appear immediately below the print as shown below >>> print(“Hello World”) Hello World >>>

  20. Understanding what Syntax Errors are and how to Avoid them • Syntax is used to describe the rules that determine the way that instructions and commands must be written. • Python IDE has syntax highlighting which automatically assigns colours to different elements • Example the “Hello World” phrase should be green. • This should help spot some simple errors when typing in commands.

  21. Understanding what Syntax Errors are and how to Avoid them Comment in Red Print in Purple Text in Green

  22. Activity • Predict what will happen with some of the following codes; • >>> print"Hello World" • >>> print("Hello World"); • >>> Print("Hello World") • >>> print("Hel World") • >>> prin(Hello World) • Write your prediction on a sheet of paper • Now try them to see what if the response you get matches up with what you predicted.

  23. 45 my_number variable name The variable’s value The box represents the space reserved in the computer’s memory What is a Variable A variable is a space in the computer’s memory where we can store data such as a number or text. The value can be changed if required. To create a variable and give it the value 45 we write: my_number=45

  24. Creating a Variable • When we create a variable we call it ‘declaring’ a variable. • When we give the variable its first value we call it ‘initialising’ a variable. • If we want to tell the computer to ‘Create a variable called “age” and give it the number 12’, we say: • age = 12 • print (age)

  25. Activity numberOne = 3 numberTwo = 4 answer = numberOne + numberTwo print (answer) • You can also perform calculations that use a variable

  26. Input • Input allow you to ask the user to input text (String), whole number (Integer ) and decimal (Floating) • Examples 1 of Input • print (“What your name?” ) • name = input () • print(“Nice to mee you” + name) # this code ask user to enter their name # Allow the user to enter their name # Output the name entered by the user. • Example 2 of Input • print (“What your first name?” ) • first_name = input() • print (“What your surname?” ) • surname = input() • print (first_name + surname ) # this code ask user to enter first name # Allow the user to enter their first name # this code ask user to enter surname # Allow the user to enter their surname # Output first name and surname entered

  27. Building First Part of Artificial Intelligence Program in Python • In this activity, you will create the first part of Artificial Intelligent and save its as AI.py. print(“What is your name?") name = input () print("Nice to meet you " + name) • This three line phrase can be developed into a very simple Artificial Intelligence. • Consider the kinds of questions and information that people ask about each other when they first meet

  28. Extending Artificial Intelligence • print("Please type your name in") • name = input () • print("Nice to meet you " + name) • print("So, " + name + ", what is your favourite food?") • favourite_food = input () • print("Ah, your favourite food is " + favourite_food) • Notice in the 4th line above it is necessary to have two + signs, one either side of the variable name. • Common errors to watch for are • Not having a complete pair of double quotation marks • Not adding + signs between variables and strings. • Task: Expand on the above code to create your own artificial Intelligence • Add a summary at the end e.g. “So, John, I know a lot about you now I know that your favourite food is chicken….

  29. Data Type and Casting • Casting is the technical term for changing the type of data you are working with. • Example casting by saying • int() =Integer • float() =floating point or decimal • str() =String or text to • Casting is used to change the data type.

  30. Integer • We can force Python to make something a whole number by saying int(number). • Predict what the following code will do: • number = 3.7 • newNumber= int(number) • print(newNumber) • Now copy the code in python and compare the answer with your prediction • Note that this won’t round the number up, it just takes the whole number part.

  31. Float • The second main type is a floating point, or just “float” for short (a decimal). • We can force Python to make something a float by saying float(number). • In group of two, predict what the following code will do: • number = 3 • print(number) • newNumber= float(number) • print(newNumber) • Now copy the code in python and compare the answer with your prediction

  32. Integer Casting Activity • Python will automatically change the data type for you a lot of the time: • In group of two, predict what the following code will do: • x = int(22) • y = int(7) • z = x/y • print(z) • Now copy the code in python and compare the answer with your prediction • Here, the two integers have provided a floating point answer and Python has changed the data type of z for you.

  33. String • A string is a block of text. • In Python we use the term “str” to mean a string. • In group of two, predict what the following code will do: • number = str(2) • print("2 + 2 = ", number + number) • Now copy the code in python and compare the answer with your prediction • This makes the value “2” into a string - so it is a word rather than a number

  34. IF Function (Selection) • Selection means selecting (or choosing) what to do next. • Should I cycle to school, or ask for a lift? • If it’s a sunny day I might cycle. • If it’s raining, I’ll ask for a lift. • Answer print (“What is the weather like today?”) answer = input() answer=str(answer) ifanswer==“sunny”: print(“I might Cycle to school”) else: print(“I’ll ask for a lift.”)

  35. IF....ELIF....ELSE • Sometimes there are more than two options. • I could walk OR cycle OR get the bus OR get a lift. • As well as IF and ELSE, we can stick an ‘ELSE IF’ (or ELIF) in the middle: • Write a pseudocode for the following code then try it. print (“What is the weather like today?”) answer = input() answer=str(answer) ifanswer==“sunny”: print(“I might Cycle to school”) elifanswer==“raining”: print(“I get the bus”) else: print(“I’ll ask for a lift.”)

  36. Quiz print("What is 2 + 2?") answer = input () answer = int(answer) if answer == 4: print("Well done") else: print("Sorry the answer was 4") • Create a new file and type in the following code:

  37. Activity • You can use a variable in a calculation, and use the same variable to store the answer. • Predict the following code: score = 112 score = score + 1 print(score)

  38. Activity • score = 0 • print("What is 2 + 2?") • answer = input () • answer = int(answer) • if answer == 4: • print("Well done") • score = score + 1 • else: • print("Sorry the answer was 4") • #this defines variable score, and sets it as zero • Modify the your game by adding the scores and comments #this increases score by one • In group, discuss what the score does and the concept of “score now equals itself plus one”

  39. LIFE CALCULATOR • Read the Life Calculator analysis • Identify all input, output and process • Make a flowchart using the symbols below Arrow

  40. Life Calculator • The system should enable the user to create a system which will calculate their expected life based on the answer they give. • The system needs to ask the user for their age, gender and name. • If they are male, their life expectancy will be 78; if they are female they will be 82. • If they are a smoker, then they lose 7 years from their lifespan. If they exercise, they gain 6 years, but lose 12 if they don’t. • Supporting Manchester United can also have an impact. If they do, the stress can take 5 years off their lives. • It would be nice if the there was a validation check to see if age was too high or low when entered. • You need to identify the input and output of this system as you design it and think about the question that you will ask.

  41. Start LIFE CALCULATOR INPUT name INPUT age End INPUT gender INPUT Life EXP Male or Female? Life EXPT= 78 M N F Man U? Y Life EXPT-5 Life EXPT= 82 Life EXPT + 6 Smoke? Life EXPT - 7 Y Y N Exercise? Life EXPT-6

  42. LIFE CALCULATOR Age Validation Start INPUT age Age > 100? Y Too Old N Age < 5? Y Too Young N End

  43. Life Calculator Python Solution

  44. Life Calculator Python Solution

  45. Life Calculator Small Basic

  46. Life Calculator Small Basic

  47. sabotage • Open your program • Swap seat with a partner • Look through your partner program • Think of two thing you can do to the program to make it stop working • Change or edit your partner code so that the whole program does not work

  48. Resources • Go on https://www.dropbox.com • Login with the following detail • Username: ahmad.jalloh@computingatschool.org.uk • Password: computing • Survey • https://www.surveymonkey.com/s/NOEFirstFeedback

  49. Conclusion • Start simple • Always design it on paper first • Can just do top level design – no details first • Iterate • Do it in small basic or Scratch first • Recreate it in Python • If you get stuck ask: CAS has a Forum – No question too simple, small or stupid • Allow students explore different solution and share with their peers and yourself • Enjoy!

More Related