Input and Output Upsorn Praphamontripong CS 1110 Introduction

  • Slides: 10
Download presentation
Input and Output Upsorn Praphamontripong CS 1110 Introduction to Programming Spring 2017

Input and Output Upsorn Praphamontripong CS 1110 Introduction to Programming Spring 2017

Program Development Cycle Design the program Write the code Correct syntax errors Test the

Program Development Cycle Design the program Write the code Correct syntax errors Test the program review Correct logic errors • Understand the task the program will perform • Break down the task into a series of steps • Algorithm • Translate an algorithm into code • Two Representation • Pseudocode • Flowchart CS 1110 2

Algorithm Pseudocode review Exercise: Get 2 numbers from the user and display the larger

Algorithm Pseudocode review Exercise: Get 2 numbers from the user and display the larger number Algorithm: 1. Get the first number 2. Get the second number 3. Compare the 2 numbers 4. Display the larger number CS 1110 Pseudocode: 1. Input the first number 2. Input the second number 3. If the first number > the second number, display the first number 4. Otherwise, display the second number (what if the first number is equal to the second number? ) 3

review Algorithm Flowchart Exercise: Get 2 numbers from the user and display the larger

review Algorithm Flowchart Exercise: Get 2 numbers from the user and display the larger number Start Algorithm: 1. Get the first number 2. Get the second number 3. Compare the 2 numbers 4. Return the larger number Input the first number Input the second number Yes the first number is greater than the second number Display the first number No Display the second number End CS 1110 4

Input – Process – Output Exercise: Get 2 numbers from the user and display

Input – Process – Output Exercise: Get 2 numbers from the user and display the larger number Input The first number The second number input() function CS 1110 Process Output Check if the first number is greater than the second number The larger number print() function 5

Reading Input from the Keyboard input(prompt) input(“What is your name? ”) • Display the

Reading Input from the Keyboard input(prompt) input(“What is your name? ”) • Display the string “What is your name? ” on the screen • Wait for the user to enter something and press Enter • The user’s input is returned as a string name = input(“What is your name? ”) • The user’s input is returned as a string and assigned to the variable name CS 1110 6

Variables • A name that represents a value stored in the computer’s memory •

Variables • A name that represents a value stored in the computer’s memory • Creating variables with assignment statements statement = "Python is awesome!!!!" Assignment operator statement Python is awesome String literal • Python is dynamic typed language statement = "Python is awesome!!!!” statement = 4 Numeric literal Type: string Type: int Variable reassignment CS 1110 7

Variable Naming Rules • No Python’s key words • No spaces • First character

Variable Naming Rules • No Python’s key words • No spaces • First character must be one of the letters (a…z or A…Z) or an underscore character ( _ ) • Case sensitive • Descriptive • Consider t (or x) vs temperature • How about payrate vs pay_rate vs pay. Rate CS 1110 8

Displaying Output on the Screen print(zero or more parameters) print(“Hello World”) • Display the

Displaying Output on the Screen print(zero or more parameters) print(“Hello World”) • Display the string “Hello World” on the screen print(“Hello ” + ”World”) • Display the string “Hello World” on the screen print(“Hello”, ”World”) • Display the string “Hello World” on the screen print() • Display an empty line CS 1110 How about print())? 9

Strings and String Literals • String = a sequence of characters • E. g.

Strings and String Literals • String = a sequence of characters • E. g. , “Hello World” • String literal = a value/string that appears in a program, the actual value of the thing it represents • E. g. , print(“Hello World”) • Must be surrounded by quotation marks • Quotation marks used • print("Python is awesome!") • print('Python is awesome!') • print('"Python is awesome!"') • print('Python's awesome!') CS 1110 10