Download presentation
Presentation is loading. Please wait.
1
Useful String Methods Cont…
2
10/5/16 Agenda Review String Methods (White boards) Add-ons
Trust Fund Buddy Program (Bad) Trust Fund Buddy Program (Good)
3
Useful string methods upper () lower() swapcase() capitalize() title()
replace(old, new, max)
4
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?
5
Whitespace means Rightspace
IndentationError: expected You’ll get this error whenever your whitespace is off.
6
Add to your notes… strip()
Returns a string where all the white space (tabs, spaces, and newlines) at the beginning and end is removed.
7
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
8
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.
9
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?
10
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?
11
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.
12
The Trust Fund Buddy – Good Program
13
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
14
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’
15
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?”))
16
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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.