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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to C Programming
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.
Lecture 3: Strings, Screen Output, and Debugging Yoni Fridman 7/2/01 7/2/01.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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.
Introduction to Python
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Introduction to C Programming
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
Python Programming Fundamentals
Fundamentals of Python: First Programs
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Input, Output, and Processing
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Flow of Control Part 1: Selection
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.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Chapter 05 (Part III) Control Statements: Part II.
CS346 Javascript -3 Module 3 JavaScript Variables.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Python Conditionals chapter 5
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Decision Structures, String Comparison, Nested Structures
Python Unit – : Operators, Expressions, and Variables
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Controlling Program Flow with Decision Structures.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
 Type Called bool  Bool has only two possible values: True and False.
Topics Designing a Program Input, Processing, and Output
Topics Introduction to Functions Defining and Calling a Void Function
Topic Pre-processor cout To output a message.
Introduction to Python
String class.
Chapter 2, Part I Introduction to C Programming
ECS10 10/10
IF statements.
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Expressions and Control Flow in JavaScript
And now for something completely different . . .
CMSC 202 Java Primer 2.
Selection Statements.
Topics Designing a Program Input, Processing, and Output
Relational Operators.
Topics Designing a Program Input, Processing, and Output
Class code for pythonroom.com cchsp2cs
Problem 1 Given n, calculate 2n
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

[Κ4] ‘Guess the Number’

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.

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.

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.

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.

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

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

Blocks - 2

Comparison Operators

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

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.

while loop -- example

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

If control statement

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

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

[Κ5] Jokes About the print() function

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='')