Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional."— Presentation transcript:

1 Module 3 Fraser High School

2 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

3 Explaining the Guess Number Program

4 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

5 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.

6 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

7 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

8 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()

9 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” http://www.sthurlow.com/python/lesson04/


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

Similar presentations


Ads by Google