Useful String Methods Cont…

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
An Introduction to Textual Programming
Bell Ringer What types are numbers are there is the python programming language?
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
Python Conditionals chapter 5
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
ECS 15 Strings, input. Outline  Strings, string operation  Converting numbers to strings and strings to numbers  Getting input  Running programs by.
Introduction to Programming
InterestRate Create an InterestRate class and InterestRateViewer client class to do the following: A person is purchasing an item with their credit card.
Course A201: Introduction to Programming 09/09/2010.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Input, Output and Variables GCSE Computer Science – Python.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
JavaScript: Conditionals contd.
Introduction to Programming
Numbers and arithmetic
Numbers and Arithmetic
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
Repetition Structures
TemperatureConversion
Intro to CS Nov 2, 2015.
Data Types and Conversions, Input from the Keyboard
Formatting Output.
Variables, Expressions, and IO
Formatting Output.
Functions CIS 40 – Introduction to Programming in Python
Introduction to Programming
getline() function with companion ignore()
Implementing Functions from a Detailed Design Quick Tips
Learning to Program in Python
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Topics Introduction to File Input and Output
Number and String Operations
Teaching London Computing
Variables Title slide variables.
Margaret Derrington KCL Easter 2014
Coding Concepts (Data- Types)
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Chapter 3 Mathematical Functions, Strings, and Objects
We are starting JavaScript. Here are a set of examples
Python programming exercise
Core Objects, Variables, Input, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Computer Science
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
Introduction to Programming
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Topics Introduction to File Input and Output
getline() function with companion ignore()
GCSE Computing.
Data Types and Expressions
Getting Started in Python
Presentation transcript:

Useful String Methods Cont…

10/5/16 Agenda Review String Methods (White boards) Add-ons Trust Fund Buddy Program (Bad) Trust Fund Buddy Program (Good)

Useful string methods upper () lower() swapcase() capitalize() title() replace(old, new, max)

Whitespace Type in the following code In Python, whitespace is used to structure code. Whitespace is important, so you have to be careful with how you use it. Whitespace consists of spaces, tabs, newlines, etc. You try it: Type in the following code oct_five = “National Do Something Nice Day” print(oct_five) What happens?

Whitespace means Rightspace IndentationError: expected You’ll get this error whenever your whitespace is off.

Add to your notes… strip() Returns a string where all the white space (tabs, spaces, and newlines) at the beginning and end is removed.

The Trust Fund Buddy This program is intended for the souls who play all day, living off a generous trust fund. The program is supposed to calculate a grand total for monthly expenditures based on user input. The grand total is meant to help those living beyond a reasonable means to stay within budget so they don’t ever have to think about getting a job. Let’s dissect this program: Go to your file that we dragged to the desktop, Go to the Chapter 2 Programs Right click trust_fund_bad Open in IDLE 3.5

The Trust Fund Buddy The program obviously isn’t working correctly… When a program produces unintended results but doesn’t crash, it has a logical error. Based on what you already know, you might be able to find out what’s happening by looking at the code.

Tracking Down Logical Errors Logical errors can be the toughest bugs to fix, since the program doesn’t crash, you don’t get the benefit of an error message to offer a clue. You have to observe the behavior of the program and investigate the code. Run the code, and make up some values for each of the expenditure prompts. What happens?

Tracking Down Logical Errors A huge number is clearly not the sum of all the numbers the user entered. But, by looking at the numbers your can see the grand total printed is a concatenation of all the numbers. How did that happen? Remember: The input() function returns a string. So each “number” the user enters is treated like a string. Which means that each variable in the program has a string value associated with it. Total = car + rent + jet + gifts + food + staff + guru + games Here we are not adding numbers, but concatenating strings! So we know the problem, how do we fix it?

Converting Values Somehow those string numbers need to be converted to numbers. Then the program will work as intended The solution to the Trust Fund Buddy is to convert the string values returned by the input function to numeric ones. Since the program works with whole dollar amounts, it makes sense to convert each string to an integer before working with it.

The Trust Fund Buddy – Good Program

Converting String to Integers Go in to the Chapter 2 folder and open the trust_fund_good.py file This program fixes the logical bug in the Trust Fund Buddy – Bad. There are several functions that convert between types. The function to convert a value to an integer is demonstrated in the lines of the Trust Fund Buddy – Good code

Type Conversion Functions Description Example Returns float(x) Return a floating-point by converting x float(“10.0”) 10.0 int(x) Return an integer value by converting x int(“10”) 10 str(x) Return a string value by converting x str(10) ’10’

Getting the User Input Name = input(“Hi. What’s your name? ”) Using the input() function, the program gets the user’s name, age, and weight: Name = input(“Hi. What’s your name? ”) Age = input (“How old are you? ”) Age = int(Age) Weight = int(input(“Okay, last question. How many pounds do you weigh?”))

Getting the User Input Remember, input () always returns a string. Since Age and Weight will be treated as numbers, they must be converted. I broke up this process in two lines for the variable Age. First, I assigned the string from input () to a variable. Then, I converted that string to an integer and assigned it back to the variable. For weight, I made the assignment one line long by nesting the function calls. I made the assignments in two different ways to remind you that you can do either. However, in practice, I’d pick one approach to be consistent.