Variables –recap-python

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
Python - Selection Starter
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Introduction to Computing Science and Programming I
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
The Pocket Protectors of Algebra
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.
Complexity (Running Time)
Yr 9 a computer science Nice to meet you variable challenge! 1.Variable assessment Looping in python 2.Looping assessment Drawing in python using turtle.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
An Introduction to Textual Programming
Control Structures FOR Statement Looping.
Python Programming Using Variables and input. Objectives We’re learning to make use of if statements to enable code to ask questions. Outcomes Build an.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
INTRODUCTION TO PYTHON. 1. The 5 operators in Python are: + - * / %
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Test 2 Review The test will consist of 3 sections. Section 1 is vocabulary matching. Section 2 is a Rate Per 100 problem. Section 3 is a Unit Rate Problem.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Variables. Todays Lesson  In todays lesson you are going to:  Learn to use variables  Learn to ask for user input  Learn to save the users response.
Course A201: Introduction to Programming 09/16/2010.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python - 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.
CS 100 Introduction to Computing Seminar October 7, 2015.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Decision Structures, String Comparison, Nested Structures
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
GCSE Computing: Programming GCSE Programming Remembering Python.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Conditionals.
Algebra Geek Patrol The Pocket Protectors of Algebra.
Algebra Geek Patrol The Pocket Protectors of Algebra.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Question Catch The teacher throws a bean bag when asking questions. This makes questioning a kinaesthetic activity and can engage pupils who don’t normally.
Input, Output and Variables GCSE Computer Science – Python.
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
GCSE COMPUTER SCIENCE Practical Programming using Python
Repetition Structures
Week of 12/12/16 Test Review.
Selection and Python Syntax
IF statements.
Lesson 3 - Repetition.
Lesson 1 Learning Objectives
Validations and Error Handling
Decision Structures, String Comparison, Nested Structures
Introduction to TouchDevelop
Module 4 Loops.
Conditional and iterative statements
A look at Python Programming Language 2018.
Topics The if Statement The if-else Statement Comparing Strings
Making decisions with code
Hint idea 2 Split into shorter tasks like this.
Starter Look at the hand-out.
Flow Control I Branching and Looping.
Presentation transcript:

Variables –recap-python Type in the following in python Then create three further variables with appropriate strings This is an assessment Variables –recap-python name = input(“What is your name? ”) print(“Hello,”,name)

Assessment point 2-python arithmetic x = int(input(“Enter a number between 1 and 100: “)) y = int(input(“Enter another number between 1 and 100: “)) print(“X + Y =”,x+y) You should now create a arithmetic program to Multiply Divide MOD You have 10 minutes to do this

example hole1 = int(input(“Enter a number between 1 and 100: “)) Hole 2 = int(input(“Enter another number between 1 and 100: “)) print(“you have the following score =”,hole1+hole2 /4) Now extend the program by working out oif the score reached by players is over par Notice I have divided the end result using the / symbol to give an average score per hole Par= amount of shots allowed for the golf hole Nb:Remember to use par==4 for example

Using if then else par = int(input(“what was your golf score ? “)) if par < 2 print(“That seems a great score you have met par for course. Well done!”) else: print(“You need more practice .”) par = int(input(“what was your golf score ? “)) if par > 2: print(“That seems over par -bad luck!”) else: print(“You have a wonderful score .”) Using the inputs from challenge 2, the “par” for the course is 11. The system should tell the user if their score was “over par” or “under par

If statements-assessment point 3 Your task now is to combine a comparative operator with an input. Computers often use if statements to determine what decision to make. Task 1. Let’s write a basic if statement using a variable first. age = int(input("What is your age?")) if age < 16: print("you are young!") else: print("your are getting old!") **Re­write this if statement to ask how many marbles I have. (Notice how the code int is in front of input because the data being entered is a number.) If the person has less than 6 marbles, it should reply “I have more than you” If the answer is else, it should reply, “Wow that’s a big collection of marbles!!

More If Statements If statements can check for a list of things instead of just 1. For example: temp = int(input("What's the temperature in celcius?")) if temp < 8: print("Brr, its a cold day!!!") elif temp >= 8 and temp < 15: print("I'ts a mild day") elif temp >= 15 and temp < 21: print("Its a warm day") elif temp >= 21: print("It's a hot day") else: print("sorry, you must enter a number") Can you see that the word if is only used once? Every other time to check if something is True, Python uses elif.

Even more if statements - 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: x = int(input(“How many hours a day do you play computer games? “) if x < 2: print(“That seems a fairly healthy balance. Well done!”) elif x < 4: print(“You’re probably good enough by now with all that practice.”) else: print(“Put the controller down and get some fresh air once in a while!”)

Even even more if statements IF ... ELIF ... ELIF ... ELIF ... ELSE You can include an unlimited number of ELIFs if you need to. Try the following: menu = "What would you like:\n\ 1. A complement?\n\ 2. An insult?\n\ 3. A proverb?\n\ 4. An idiom?\n\ 9. Quit\n" x = int(input(menu)) if x == 1: print("You look lovely today!") elif x == 2: print("You smell funny.") elif x == 3: print("Two wrongs don't make a right. But three lefts do...") elif x == 4: print("The pen is mightier than the sword.") elif x == 9: print("Goodbye!!!") There are a couple of important bits here: • You can put a line break in your string by using “\n”. • You can continue a line of code by putting a “\” at the end. • If you are testing for equality, use a double equals (is 3x2 == 6?)

If statements -Task 1 Write a programme to check if someone’s name is John, George, Ringo or Paul. If one of those names is True, Python should say “Hey that’s the name of a Beatle!”. If not, Python should say “That’s a nice name”. Write your answer in the next box

Task 2 if statements Write a programme to comment on how interesting a football match was. ❏ If no goals were scored, Python should say “the game was a bore draw!” ❏ 1­2 goals: “Not the most interesting game” ❏ 3­5 goals: “It was a very interesting ❏ 6+ goals: “The football match was an unmissable game!”

Assessment 3-if statements Using the inputs from challenge 2, the “par” for the course is 11. The system should tell the user if their score was “over par” or “under par” 4 – Else IF Again using the inputs from question 2, give the user some feedback. If their score was: Less than 11 then say “Well done, you should go pro” Equal to 11 then say “Well done, you made par” More than 11 then say “More practice needed”

Looping-while loops Computers can be programmed to continue repeating code while a condition is True. Look at the example below for an idea name = "" while name != "Batman": print("Somebody call Batman!") print("Are you him? ") name = input("What's your name?") As you can see in the example in the left, the code will keeping looping and asking the same question while the answer is not Batman. Notice the nesting of statements underneath the while? It is the indented code that will keep looping

Task 1 Write a while loop to ask someone what the best football team in the world is. Repeat the question until they say the correct team.

Harder loops! Now we will try to write one more while loop this time using a condition. Again this task is a lot harder. Look at the example below and try to write your own. secret_number = 6 guesses = 3 while guesses > 0: print("Can you guess what number I am thinking?) guess = int(input("write a number ")) if guess == secret_number: print("Well done, you guessed correctly!") break else: guesses = guesses ­1 if guesses == 0: print("You have no guesses left!") In this code, we also learn of the break statement. This will tell the PC to skip to the next line of code that is not in the current indented nesting.

Loops harder still The code below shows an example of how to add a list of numbers together: count = 5 number = 0 print("this programme will work out the total of a list of numbers") while count > 0: number = number + int(input(print("Please enter a number :"))) count = count ­ 1 print(number)

Loop assessment 1 5 – For Loops Write a for loop that will print the following sentence 20 times: “I must listen to my IT teachers at all times, because they are very intelligent”

Loop assessment 2 Copy the code for challenge 5, but this time ask the user to input how many times they would like that sentence printed. The for loop should then print only than number of times the sentence: “I must listen to my IT teachers at all times, because they are very intelligent”

While loops assessment Ask the user if they would like some advice (Yes or No). Write a while loop that will continue asking the user if they want advice until they answer “Yes”. At which point the following must be printed to the screen: “Always list to your IT teachers Note that the program should accept both “Yes” or “yes” for the answer

While loops extension task A game of cricket has 6 balls per over. Write a while loop that asks for the user to input the score of each bowl and at the end will tell you the score for the over. For example (0,2,0,0,4,6 would total to 26 for the over)

And now for pygame! See pygame tutorial