Input from the Console This video shows how to do CONSOLE input.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Advertisements

Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Python Conditionals chapter 5
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introduction to Programming
Python Exceptions and bug handling Peter Wad Sackett.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Input, Output and Variables GCSE Computer Science – Python.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
Introduction to Programming
Python Basics.
Chapter 2 Writing Simple Programs
Basic concepts of C++ Presented by Prof. Satyajit De
C++ First Steps.
GCSE COMPUTER SCIENCE Practical Programming using Python
Agenda Introduction Computer Programs Python Variables Assignment
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
Using the Console.
Input and Output Upsorn Praphamontripong CS 1110
Lesson 1 - Sequencing.
TemperatureConversion
Example: Vehicles What attributes do objects of Sedan have?
Introduction to Python
Intro to CS Nov 2, 2015.
Data Types and Conversions, Input from the Keyboard
Objectives You should be able to describe: Interactive Keyboard Input
Programming Fundamentals
Implementing Functions from a Detailed Design Quick Tips
A First Book of ANSI C Fourth Edition
Introduction to Programming
getline() function with companion ignore()
Implementing Functions from a Detailed Design Quick Tips
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Imperative Programming
Ken D. Nguyen Department of Computer Science Georgia State University
Python Primer 2: Functions and Control Flow
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
And now for something completely different . . .
Introduction to C++ Programming
Validations and Error Handling
Exception Handling.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
We’re moving on to more recap from other programming languages
ERRORS AND EXCEPTIONS Errors:
Topics Designing a Program Input, Processing, and Output
Python programming exercise
Advanced Python Concepts: Exceptions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Advanced Python Concepts: Exceptions
Terminal-Based Programs
Introduction to Programming
“Everything Else”.
Ken D. Nguyen Department of Computer Science Georgia State University
Exception Handling COSC 1323.
CSE 231 Lab 5.
Topics Introduction to File Input and Output
Copyright (c) 2017 by Dr. E. Horvath
CS 1111 Introduction to Programming Spring 2019
Lecture 1 Review of 1301/1321 CSE /26/2018.
getline() function with companion ignore()
Imperative Programming
Data Types and Expressions
Flow Control I Branching and Looping.
Getting Started in Python
Dealing with Runtime Errors
Presentation transcript:

Input from the Console This video shows how to do CONSOLE input. http://xkcd.com/1586/ print('My name is Zelda.') print('Hi, ' + s + '!') s = input('What is your name: ') This video shows how to do CONSOLE input. A subsequent video does the same for FILE input.

The input function always returns a STRING. Input from the Console The input function displays its optional argument on the Console and waits for the user to: Type a sequence of characters, and Press the ENTER key. The input function returns the sequence of characters that the user typed, as a string. s = input('What is your name: ') print('Hi, ' + s + '!') Indicates that the program is still running. Here, waiting for user to type … and press ENTER. The input function always returns a STRING. The newline character corresponding to the ENTER key is NOT included in the string.

Input from the Console s = input('Continue? (y/n) ') Optional argument: a PROMPT that is printed on the Console s = input('Continue? (y/n) ') The input function always returns a STRING. The newline character corresponding to the ENTER key is NOT included in the string. The prompt usually includes a space at its end to help the user separate the prompt from what the user herself types.

The input function always returns a STRING. Input from the Console Use the int function to convert a STRING into its corresponding INTEGER The input function always returns a STRING. s = input('How old are you? ') age = int(s) if age >= 18: <allow the user to vote> age_in_months = 12 * age Composition: a function acting on the returned value from another function. s = int(input('How old are you? '))

The input function always returns a STRING. Use the int function to convert a STRING into its corresponding INTEGER. Use the float function to convert a STRING into its corresponding FLOATING POINT number. Input from the Console s = int(input('How old are you? ')) money = float(input('How much money do you have? ')) The input function always returns a STRING.

Input from the Console The int and float functions ATTEMPT to convert their argument to a number money = float(input('How much money do you have? ')) while True: try: money = float(input('How much money ...? ')) break except ValueError: print('Enter a NUMBER, like 12.52. Try again.') print() Another video shows how to HANDLE such EXCEPTIONS, like this:

You can process the string that input returns Input from the Console You can process the string that input returns s = input('Hit? (y/n) ') if s.strip().lower().startswith('y'): is_hit = True else: is_hit = False s = input('Hit? (y/n) ') is_hit = s.strip().lower().startswith('y') s = ' Hello, HOW r YoU?! \n Fine! \n\n ' s.strip()  'Hello, HOW r YoU?! \n Fine!' s = 'yes it does' s.startswith('y')  True s = 'nyyyyyyyy' s.startswith('y')  False s = ' Hello, HOW r YoU?!' s.lower()  ' hello, how r you?!'

You can process the string that input returns Input from the Console You can process the string that input returns s = input('Hit? (y/n) ') is_hit = s.strip().lower().startswith('y')

Input from the Console: Summary input to get input from the user via the Console int to convert a STRING into its corresponding INTEGER float to convert a STRING into its corresponding FLOATING POINT number The input function always returns a STRING. name = input('Enter your name: ') age = int(input('How old are you? ')) money = float(input('How much money do you have? ')) Next videos show how to: Do FILE input Handle EXCEPTIONS that occur when doing input and other operations