PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

Slides:



Advertisements
Similar presentations
EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
Advertisements

Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Debugging Introduction to Computing Science and Programming I.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Computing Theory: BBC Basic Coding Year 11. Lesson Objective You will: Be able to define what BBC basic is Be able to annotate BBC basic code Be able.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
From Scratch to Python Learn to program like the big boys / girls!
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
An Introduction to Textual Programming
Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 9, 2015)
Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 17, 2015)
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Introduction to Python Lesson 1 First Program. Learning Outcomes In this lesson the student will: 1.Learn some important facts about PC’s 2.Learn how.
Files Tutor: You will need ….
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
CRE Programming Club - Class 2 Robert Eckstein and Robert Heard.
Python Let’s get started!.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Getting Started with HTML. HTML  Hyper Text Markup Language  HTML isn’t a program language, its known as a markup language  A Markup language has tags.
Python Lesson 2.
GCSE Computing: Programming GCSE Programming Remembering Python.
Getting Started With Python Brendan Routledge
Introducing Python 3 Introduction to Python. Introduction to Python L1 Introducing Python 3 Learning Objectives Know what Python is and some of the applications.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Hackety Hack! Written by Krystal Salerno Presented by _______________.
Introducing Python Introduction to Python.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Whatcha doin'? Aims: To start using Python. To understand loops.
A Playful Introduction to Programming by Jason R. Briggs
Lesson 1 - Sequencing.
PYGAME.
Python Let’s get started!.
Formatting Output.
Lesson 1 An Introduction
IF statements.
Formatting Output.
Engineering Innovation Center
Computer Science and an introduction to Pascal
File Handling Programming Guides.
Introduction to TouchDevelop
A look at Python Programming Language 2018.
Python programming exercise
Beginning Python Programming
Input and Output Python3 Beginner #3.
Introduction to Python
Iteration – While Loops
Presentation transcript:

PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

CHALLENGE If you know how to make games in Python, your task it to surprise us with what you can come up with. If not we are going to create a few games together in Python now.

PYTHON CAN DO SIMPLE MATHS Python uses these symbols to do simple Maths, what do you think each one does? +-/*+-/*

VARIABLES What’s a variable? Something that is stored for later use and can be called if needed. Variables can be any data type.

LETS GIVE IT A TRY

STRINGS Variables can contain text too, these are called strings. We use quote marks to set what text the variable will hold. Variable = ‘ text ‘

LETS GIVE IT A TRY

LETS MAKE OUR FIRST SIMPLE PROGRAM

WHAT WILL IT DO? Together we will make a program to say hello and ask for a name. It will then use the name that is inputted to say, it is good to meet you (name).

SHALL WE SHOW YOU WHAT YOU WILL BE CREATING?

STEP 1 Click on File, New Window. This is the File Editor. Click Save and save it as Hello World in your user area. Make a folder called Python to put it in if you want.

# TAGS A # tag can be used to write a comment to anyone who reads your code. Try this now, tell the reader what the program will be doing.

PRINT COMMAND What do you think a print command might do? It prints whatever you ask it to onto the screen of the user. Just like strings the text needs to have quote marks around it but also needs to have brackets ( ) outside of them too.

LETS GIVE IT A TRY

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS… # This program says hello and asks my name. print (‘Hello World’) print (‘What is your name?’)

ADDING A VARIABLE Now we need a variable to hold a name value.

INPUT COMMAND The input command will take what the user inputs into the program. It can be used to put inputted text into a variable. It looks like this: Name = input ()

LETS GIVE IT A TRY

NOW WE CAN USE THE NAME TO SAY HELLO… Try to work out how you might do this. You need the print command, the + symbol, the variable and the other symbols needed in each command e.g. ‘ ‘ ( ).

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS… # This program says hello and asks my name. print (‘Hello World’) print (‘What is your name?’) name = input() print (‘It is good to meet you, ‘ + name)

LET’S RUN THE PROGRAM! Save the program again. Click Run, Run Module. The program should start. If it errors there is a mistake in your code, try to fix it yourself, if you cant ask a friend, as a last resort ask a teacher.

LETS MAKE A HARDER GAME…

WHAT WILL IT DO? This game will ask the users name and ask them to play a guess the number game. The user will have 6 guesses, but they will be told if they are guessing too high or low. Once they guess correctly it will tell them how many tries it took to get it correct.

SHALL WE SHOW YOU WHAT YOU WILL BE CREATING?

STEP 1 Click on File, New Window. This is the File Editor. Click Save and save it as Guess the Number in your user area. Make a folder called Python to put it in if you want.

# TAG TIME Write a # tag comment to anyone who reads your code.

IMPORT Type import random This imports several functions related to random numbers. We will come back to this later.

VARIABLE Set a variable guessesTaken to 0.

ASK FOR THEIR NAME Ask for their name and remember to tell the computer to add it to a variable called name using, input()

SETTING THE NUMBER TO BE FOUND To do this we need a variable called number and the random statement again. Type in the following: number = random.randint (1, 20) This asks for the computer to import a random integer (number) between 1 and 20.

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS… # This program is a guess the number game. import random guessesTaken = 0 print (‘Hello! What is your name?’) name = input() number = random.randint (1, 20)

USE THE NAME VARIABLE Use the name variable to tell the player you are thinking of a number between 1 and 20.

WHILE LOOPS What is a while loop? A while loop will perform an action (set by you) while a condition is being met (also set by you). We need a while loop that asks the player to take a guess while the number of guesses taken is less than 6.

WHILE LOOPS while guessesTaken <6: print (‘Take a guess’) # indent this line by 4 spaces

GUESS VARIABLE Now we need a guess variable that will contain what the user inputs. How might we do this?

SETTING THE INPUT TO AN INTEGER The input at the moment will be classed as a string of text, even though we know it is a number the computer does not.

SETTING THE INPUT TO AN INTEGER We need to tell the computer that the inputted data is an integer. This is how we do it: guess = int(guess)

TELL THE COMPUTER THE USER HAS MADE A GUESS Now the computer needs to increase the variable guessesTaken by 1. How might we do this? guessesTaken = guessesTaken + 1

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS… number = random.randint (1, 20) print (‘Well, ‘ + name + ‘, I am thinking of a number between 1 and 20.’)

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS… while guessesTaken < 6: print (‘Take a guess’) guess = input() guess = int(guess) guessesTaken = guessesTaken + 1 To tell the computer the while loop is still running we must indent all of the content by 4 spaces.

IF STATEMENTS An if statement will tell the computer to do something if a condition has been met, if it has not it wont compete the command set by you. There are other types of if statements. Can you think of any?

IF STATEMENTS if guess < number: print (‘Your guess is too low’) if guess > number: print (‘Your guess is too high’) if guess == number: break == means equal to The if statements are indented by 4 spaces and the contents of the if statements by another 4 spaces.

THE WHILE LOOP IS OVER! All code should now start without an indent.

SO ONCE THE GUESS LIMIT IS REACHED, WHAT HAPPENS?

IF THEY GUESSED CORRECTLY Think how we could use an if statement to do a command if they guessed correctly. if guess == number:

WHAT WOULD THE COMPUTER DO? First we need the convert the number of guesses back into a string so we can display it for the user. How might we do this? guessesTaken = str(guessesTaken)

THEN CONGRATULATE THEM AND TELL THEM THEIR GUESSES print (‘Good job, ‘ + name + ‘! You guessed the number in ‘ + guessesTaken + ‘guesses!’)

IF THEY GUESSED INCORRECTLY Think how we could use an if statement to do a command if they guessed correctly. != means not equal to. if guess != number:

WE CONVERT THE NUMBER VARIABLE This time we convert the random number chosen at the start into a string so we can display it. How might we do this? number = str(number)

THEN TELL THEM THEY WERE NOT SUCCESSFUL!  print (‘Nope, the number I was thinking of was ‘ + number)

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS… if guess == number: guessesTaken = str(guessesTaken) print (‘Good job, ‘ + name + ‘! You guessed the number in ‘ + guessesTaken + ‘guesses!’) Notice the indents!

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS… if guess != number: number = str(number) print (‘Nope, the number I was thinking of was ‘ + number) Notice the indents!

CONGRATULATIONS YOU HAVE MADE YOUR GUESS THE NUMBER GAME!

LET’S RUN THE PROGRAM! Save the program again. Click Run, Run Module. The program should start. If it errors there is a mistake in your code, try to fix it yourself, if you cant ask a friend, as a last resort ask a teacher.