Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Introduction to Computing Science and Programming I
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Conditional Statements Introduction to Computing Science and Programming I.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
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.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Fundamentals of Python: From First Programs Through Data Structures
Python.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
An Introduction to Textual Programming
Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
Control Structures FOR Statement Looping.
Programming Training Main Points: - Python Statements - Problems with selections.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
If statements while loop for loop
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
University of Sunderland CIF 102/FIF102 Fundamentals of DatabasesUnit 15 Programming in Microsoft Access using VBA Using VBA to add functionality.
ORDER OF CONTENT AND INSTRUCTIONS A program in its simplest form usually contains three kinds of activity:  INPUT : The program asks the user for some.
Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program.
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.
Course A201: Introduction to Programming 09/16/2010.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
Algorithms Writing instructions in the order they should execute.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decision Structures, String Comparison, Nested Structures
Python Unit – : Operators, Expressions, and Variables
Python Functions : chapter 3
Loops and Simple Functions CS303E: Elements of Computers and Programming.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Copyright © Curt Hill The C++ IF Statement The most important decision statement Part 1.
Controlling Program Flow with Decision Structures.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Getting Started With Python Brendan Routledge
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.
Main Points: - Python Statements - Problems with selections.
Repetition Structures
ECS10 10/10
Variables, Expressions, and IO
Engineering Innovation Center
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Coding Concepts (Basics)
Selection Statements.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Introduction to Computer Science
Another Example Problem
def-ining a function A function as an execution control structure
Presentation transcript:

Module 3 Fraser High School

Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional statements using comparison operators use if statements cast variables using str() and int() functions

Explaining the Guess Number Program

Import Random import random The import command imports external modules that Python has access too. These modules have built in functions that allows us to tap into extra code we can use without having to write it all. There are hundreds of modules that can be used. To access the modules you can use Import module – for example import random Or From module import name for example from math import sqrt this imports the square root function from the math module

Setting up the variables These lines of code assigns the variable guessesTaken the value 0 This prints a welcome screen assigning the variable myName the user input The final part assigns the variable number with a value the computer generates using the randint (random integer) function from the random module. The computer generates a random integer between 1 and 20. Printing the next line concatenating the text with the variable myName.

The Loop 1.This section of the code sets up a block of code which will loop while the variable guessesTaken is less than 6 2. The computer asks the user for the variable guess (this is the users input) The variable is then cast as an integer 3. The variable guessesTaken is increased by 1 4. The computer compares the users input guess with the computers number – feeding back to the user if the number is too low or too high. 5. If the two number are the same the code breaks from the loop and continues to flow onto the next command

Finishing it off If you guess the right number that is guess ==number then the computer will return the statement showing how many guesses you took If you did not guess the right number that is guess != number then the computer will return the statement showing you what the number was

Summary – what instructions did you use Expressions – unlike statements expressions return something (print 'Nope you got it wrong my number was ' + number) Assignment statements – simply put we gave value to variables (guessesTaken=0) Flow Control Statements – if, while and break. They decide which statements are executed. Normal flow is to start from the top and work down, if, while and breakstatements alter this flow. if guess == number: break Functions – print() and input() myName=input()

Exercise – Loop practice Simple While loop example a = 0 while a < 10: a = a + 1 print a In Plain English 'a' now equals 0 As long as 'a' is less than 10, do the following: Make 'a' one larger than what it already is. print on-screen what 'a' is now worth. while {condition that the loop continues}: {what to do in the loop} {have it indented, usually four spaces} {the code here is not looped} {because it isn't indented} #EXAMPLE #Type this in, see what it does x = 10 while x != 0: print x x = x - 1 print "wow, we've counted x down, and now it equals", x print "And now the loop has ended." Try this and see how it works: When you get this try the module 3 exercise – “Table Practice”