Presentation is loading. Please wait.

Presentation is loading. Please wait.

Μαθαίνοντας Python -- 2. [Κ4] ‘Guess the Number’

Similar presentations


Presentation on theme: "Μαθαίνοντας Python -- 2. [Κ4] ‘Guess the Number’"— Presentation transcript:

1 Μαθαίνοντας Python -- 2

2 [Κ4] ‘Guess the Number’

3 Topics import statements Modules Arguments while statements Conditions Blocks Booleans Comparison operators The difference between = and ==. if statements The break keyword. The str() and int() functions. The random.randint() function.

4 The import statement import random Statements Statements are instructions that perform some action but do not evaluate to a value like expressions do Module Modules are Python programs that contain additional functions. We can use the functions of these modules by bringing them into our programs with the import statement.

5 The random.randint() function number = random.randint(1, 20) Because the randint() function is provided by the random module, we precede it with random. (don't forget the period!) to tell our program that the function randint() is in the random module.

6 Passing Arguments to Functions number = random.randint(1, 20) The integer values between the parentheses in the random.randint(1, 20) function call are called arguments. Arguments are the values that are passed to a function when the function is called.

7 Blocks & Indentation while guessesTaken < 6: print('Take a guess.') # There are four spaces in front of print. guess = input() guess = int(guess) guessesTaken = guessesTaken + 1 if guess < number: print('Your guess is too low.') # There are eight spaces in front of print. if guess > number: print('Your guess is too high.') if guess == number: break

8 Blocks - 1 A block is one or more lines of code grouped together with the same minimum amount of indentation. You can tell where a block begins and ends by looking at the line's indentation (that is, the number of spaces in front of the line).

9 Blocks - 2

10 Comparison Operators

11 Conditions A condition is an expression that combines two values with a comparison operator (such as ) and evaluates to a Boolean value. A condition is just another name for an expression that evaluates to True or False. Notice the difference between the assignment operator (=) and the “equal to” comparison operator (==)

12 while loop statement The while statement marks the beginning of a loop. When the execution reaches a while statement, it evaluates the condition next to the while keyword. If the condition evaluates to True, the execution moves inside the while-block.

13 while loop -- example

14 Strings to Integers int() function will take the string value we give it and return the integer value form of it. guess = int(guess)

15 If control statement

16 If examples if guess > number: print('Your guess is too high.') if guess == number: break The line inside the if-block is a break statement that tells the program to immediately jump out of the while-block to the first line after the end of the while-block

17 Types of instructions Expressions which are made up of values connected by operators Assignment statements which simply store values in variables flow control statements if, while and break they decide which instructions are executed. I/O commands Print(), input()

18 [Κ5] Jokes About the print() function

19 Making the most of print() Backslash Escape characters Double vs. Single Quotes end keyword, end=‘ ‘ The print() function automatically appends a newline character to the end of the string we pass it to be displayed on the screen. To change this, we pass the end keyword argument with a blank string. print('spam', end='')

20


Download ppt "Μαθαίνοντας Python -- 2. [Κ4] ‘Guess the Number’"

Similar presentations


Ads by Google