Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 • 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 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. INTRODUCTION TO SMALL BASIC How to Open Small Basic • Start All Program Microsoft Small Basic Run the program Code Area Execution Mode Area 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”) 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 ) 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 1. Jabberwacky: http://www.jabberwacky.com/ 2. Cleverbot: http://www.cleverbot.com/ 3. Elbot: http://www.elbot.com/ 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" ) 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() If Answer = "4" Then TextWindow.WriteLine ("Well done " + Name) Else TextWindow.WriteLine("That is the wrong answer" + Name + ", the answer was 4") EndIf 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. The variable’s value variable name 45 To create a variable and give it the value 45 we write: my_number=45 The box represents the space reserved in the computer’s memory ACTIVITY You can also perform calculations that use a variable numberOne = 3 numberTwo = 4 answer = numberOne + numberTwo TextWindow.WriteLine(answer) 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) 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() If Answer = "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) 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 goto statement 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 Goto startagain EndIf 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 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 ADDING RESPONSIVE FEEDBACK • Solution If score = 5 Then TextWindow.WriteLine ("Excellent work " + Name + " Your score is: " + score) ElseIf score > 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 INTRO TO PYTHON PROGRAMMING CPD 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 >>> 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. UNDERSTANDING WHAT SYNTAX ERRORS ARE AND HOW TO AVOID THEM Print in Purple Text in Green Comment in Red ACTIVITY • Predict what will happen with some of the following codes; 1. >>> print"Hello World" 2. >>> print("Hello World"); 3. >>> Print("Hello World") 4. >>> print("Hel World") 5. >>> 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. 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. The variable’s value variable name 45 To create a variable and give it the value 45 we write: my_number=45 The box represents the space reserved in the computer’s memory 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) ACTIVITY You can also perform calculations that use a variable numberOne = 3 numberTwo = 4 answer = numberOne + numberTwo print (answer) 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?” ) # this code ask user to enter their name name = input () # Allow the user to enter their name print (“Nice to mee you” + name) # Output the name entered by the user. Example 2 of Input print (“What your first name?” ) # this code ask user to enter first name first_name = input () # Allow the user to enter their first name print (“What your surname?” ) # this code ask user to enter surname surname = input () # Allow the user to enter their surname print (first_name + surname ) # Output first name and surname entered 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 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…. 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. 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. 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 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. 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 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) if answer == “sunny”: print(“I might Cycle to school”) else: print(“I’ll ask for a lift.”) 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) if answer == “sunny”: print(“I might Cycle to school”) elif answer == “raining”: print(“I get the bus”) else: print(“I’ll ask for a lift.”) QUIZ Create a new file and type in the following code: print("What is 2 + 2?") answer = input () answer = int(answer) if answer == 4: print("Well done") else: print("Sorry the answer was 4") 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) ACTIVITY Modify the your game by adding the scores and comments score = 0 #this defines variable score, and sets it as zero print("What is 2 + 2?") answer = input () answer = int(answer) if answer == 4: print("Well done") score = score + 1 #this increases score by one else: print("Sorry the answer was 4") In group, discuss what the score does and the concept of “score now equals itself plus one” LIFE CALCULATOR • Read the Life Calculator analysis • Identify all input, output and process • Make a flowchart using the symbols below Arrow 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. LIFE CALCULATOR Start INPUT name End INPUT age INPUT Life EXP INPUT gender N Life EXPT-5 Y N Life EXPT-6 Male or Female? Man U? F Life EXPT= 82 Life EXPT + 6 Y Smoke? Exercise ? M Life EXPT= 78 Y Life EXPT - 7 LIFE CALCULATOR AGE VALIDATION Start INPUT age Age > 100? N Y Too Old Y Age < 5? N End Too Young LIFE CALCULATOR PYTHON SOLUTION LIFE CALCULATOR PYTHON SOLUTION LIFE CALCULATOR SMALL BASIC LIFE CALCULATOR SMALL BASIC 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 RESOURCES • Go on https://www.dropbox.com • Login with the following detail • Username: [email protected] • Password: computing Survey • https://www.surveymonkey.com/s/NOEFirstFeedback 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!