Info Bjn

pay input 'How much were you paid this week 1 print 'I will deposit that into your account.' savings.deposit pay cash input 'How much would you like to withdraw 1 print 'I will withdraw that from your account.' savings.withdraw cash Display the balance. print savings Program Output with input shown in bold Enter your starting balance 1000.00 F.nter How much were you paid this week 50C.0f filter I will deposit that into your account. ' from y lt The account balance is i gt juu.ao. The name of...

Review Questions Ljq

1. This term refers to an individual item in a list. 2. This is a number that identifies a character in a string, or an item in a list. 3. This is the first index in a string or a list. d. The size of the list minus one 4. This is the last index in a string or a list. d. The size of the string or list minus one 5. This will happen if you try to use an index that is out of range for a list. a. a ValueError exception will occur b. an IndexError exception will occur c. The string or list will be...

Info Fno

The show-interest function displays the amount of 10 simple interest for a given principal, interest rate per period, and number of periods. def show_interest principal, rate, periods interest principal rate periods print 'The simple interest will be .2f.' interest The simple interest will be 1000.00. Notice in line 7 that the order of the keyword arguments does not match the order cf the parameters in the function header in line 13. Because a keyword argument specifies which parameter the...

Info Fgj

This program opens the file in line 6, and then in line 10 reads the first field of the first record. This will be the first employee's name. The while l oop in line 13 tests the value to determine whether it is an empty string. If it is not, then the loop iterates. Inside the loop, the program reads the record's second and third fields the employee's ID number and department , and displays them. Then, in line 32 the first field of the next record the next employee's name is read. The loop...

Info Nmd

F is rhe Fahrenheit temperature and C is the Celsius temperature. A county collects property taxes on the assessment value of property, which is 60 percent of the property's actual value. If an acre of land is valued at 10,000, its assessment value is 6,000. The property tax is then 0.64 for each 100 of the assessment value. The tax for the acre assessed at 6,000 will be 38.40. Write a GUI program that displays the assessment value and property rax when a user enters the actual value of a...

Decision Structures and Boolean Logic

Info Qxj

Notice that the__init__method has two parameter variables self and bal. The bal parameter will accept the account's starting balance as an argument. In line 10 the bal parameter amount is assigned to the object's__balance attribute. The deposit method is in lines 15 through 16. This method has two parameter variables self and amount. When the method is called, the amount that is to be deposited into the account is passed into the amount parameter. The value of the parameter is then added to...

Info Ffc

The program should ask the user to enter a temperature in Celsius, and then display the temperature converted to Fahrenheit. Last month Joe purchased some stock in Acme Software, inc. Here are the derails of the purchase The number of shares that Joe purchased was 1,000. When Joe purchased the stock, he paid 32.87 per share. Joe paid his stockbroker a commission that amounted to 2 percent of the amount he paid for the stock. Two weeks later Joe sold the stock. Here are the details of the sale...

CPU Azs

This process is repeated for each high-level instruction. The statements that a programmer writes in a high-level language are called source code, or simply code. Typically, the programmer types a program's code into a text editor and then saves the code in a file on the computer's disk. Next, the programmer uses a compiler to translate the code into a machine language program, or an interpreter to translate and execute the code. If the code contains a syntax error, however, it cannot be...

Info Cot

car vehicles.Car 'BMW , 2001, 70000, 15000.0, 4 Create a Truck object for a used 2 002 Toyota pickup with 40,000 miles, priced at 12,000, with 4-wheel drive. at 18,500, with 5 passenger capacity. suv vehicles.SUV 'Volvo', 2000, 30000, 18500.0, 5 print 'The following car is in inventory ' print ' Make ' , car. getjrnake print 'Model ', car.get_model Create a Car object for a used 2 001 BMW with 70,000 miles, priced at 15,000, with

Introduction to ValueReturning Functions Generating Random Numbers

CONCEPT A value-returning function is a function that returns a value back to the part of the program that called it. Python, as well as most other programming languages, provides a library of prewritten functions that perform commonly needed tasks. These libraries typically contain a function that generates random numbers. In Chapter 3 you learned about simple functions. A simple function is a group of statements that exist within a program for the purpose of performing a specific task. When...

Info Atq

14 print ' The sale price is', sale_price Program Output with input shown in bold Enter the item's original price 100.00 Enter The sale price is 80.0 Be careful when dividing an integer by another integer. In Python, as well as many other languages, when an integer is divided by an integer the result will also be an integer. This behavior is known as integer dzvision. For example, look at the following statement What value will the number reference after this statement executes You would...

Designing a Program to Use Functions

CONCEPT Programmers commonly use a technique known as top-down design to break down an algorithm into functions. Flowcharting a Program with Functions In Chapter 2 we introduced flowcharts as a tool for designing programs. In a flowchart, a function call is shown with a rectangle that has vertical bars at each side, as shown in Figure 3-8. The name of the function that is being called is written on the symbol. The example shown in Figure 2-8 shows how we would represent a call to the message...

Must-read Books On Python

13 Display the amount needed to deposit. 14 print 'You will need to deposit this amount ', present_value Program Output with input shown in bold Enter the desired future value 10000.0 Enter Enter the annual interest rate 0.05 Enter Enter the number of years the money will growi 10 Enter You will need to deposit this amount 6139.13253541 NOTE Unlike the output shown for this program, dollar amounts are usually rounded to two decimal places. Later in this chapter you will learn how ro format...

Info Dfn

11 print 'I am changing the value.' The main function creates a local variable named value in line 5, assigned the value 99. The print statement in line 6 displays ' The value is 99'. The value variable is then passed as an argument to the change-me function in line 7. This means that in the change_me function the arg parameter will also reference the value 99. This is shown in Figure 3-17. 3-87 The value variable is passed to the change-me function print 'Back in main the value is', value...

Cpu 1

Fetch the next instruction in the program. Decode the instruction 2 to determine which operation to perform. Execute the instruction perform the operation . 10111000 10011110 00011010 11011100 and so forth. Execute the instruction perform the operation . From Machine Language to Assembly Language Computers can only execute programs that are written in machine language. As previously mentioned, a program can have thousands or even millions of binary instructions, and writing such a program would...

Info Xgo

def__init__ self, account_num, int_rater bal The following methods are mutators for the def set_account_num self, account_num self. account num account num def set_interest_rate self, int_rate self. interest rate int rate def set_balance self, bal self. balance bal The following methods are accessors for the The class's__init__method appears in lines 9 through 12. The__init__method accepts arguments for the account number, interest rate, and balance. These arguments are used to initialize data...

Info Koo

input validation loops, 180 mainline, 82 running total calculation, 173 while loop, 153 Logic errors, 32 Logical operators. See also Operators and, 138,139 compound Boolean expressions using, 138 defined, 138 not, 138, 139-140 numeric ranges with, 143 or, 138,139-140 Long statements, breaking, 61-62 152-161 count-controlled, 152 defined, 152 in file processing, 242-249 for, 161-172,245-246 infinite, 159 input validation, 179-184 nested, 184-186 pretest, 156-157 recursion versus, 413 sentinels...

Info Lsf

Program Output with input shown in bold Enter your annual salary 35000 Enter Enter the number of years on your current job 1 Enter You qualify for the loan. Program Output with input shown in boid Enter your annual salary 25000 Enter Enter the number of years on your current job 5 Enter You qualify for the loan. Program Output with input shown in bold Enter your annual salary 12000 Enter Enter the number of years on your current job 1 Enter You do not qualify for this loan.

Programming Exercises Drm

Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows To keep the function simple, assume that x and y will always hold positive nonzero integers. Write a recursive function that accepts an integer argument, n. The function should display n...

More About Data Output

When this statement executes, it displays Python recognizes several escape characters, some of which are listed in Table 2-7. Table 2-7 Some of Python's escape characters n Causes output to be advanced to the next line. t Causes output to skip over to the next horizontal tab position. Causes a single quote mark to be printed. Causes a double quote mark to be printed. Causes a backslash character to be printed. The t escape character advances the output to the next horizontal tab position. A tab...

Info Bfd

6 print 'Enter the names of three friends.' 7 namel raw-input 'Friend 1

Using the Target Variable Inside the Loop

In a for loop, the purpose of the target variable is to reference each item in a sequence of items as the loop iterates. In many situations it is helpful to use the target variable in a calCulation or other task within the body of the loop. For example, suppose you need to write a program that displays the numbers 1 through 10 and their squares, in a table similar to the following

Programming Exercises Mzs

Assume that a file containing a series of integers is named numbers . txt and exists on the computer's disk. Write a program that displays all of tlie numbers in the file. Write a program that asks rhe user for rhe name of a file. The program should display only the first five lines of the file's contents. If the file contains less than five lines, it should display the file's entire contents. Write a program that asks the user for the name of a file. The program should display the contents of...

H Vik

A GUI program presents a window with various graphical widgets that the user can interact with or view. The Tkinter module provides 15 widgets, which are described in Table 12-1. We won't cover all of the Tkinter widgets in this chapter, but we will demonstrate how to create simple GUI programs that gather input and display data. Table 12-1 Tkinter Widgets Widget Description A button that can cause an action to occur when it is clicked. A rectangular area that can be used to display graphics. A...

Checkpoint Ozw

12.2 How does a command line interface work 12.3 When the user runs a program in a text-based environment, such as the command line, what determines the order in which things happen 12.4 What is an event-driven program CONCEPT In Python you can use the Tkinter module to create simple GUI programs. Python does not have GUI programming features built into the language itself. However, it comes with a module named Tkinter that allows you to create simple GUI programs. The name Tkinter is short for...

Programming Exercises Rhd

Write a program that gets a string containing a person's first, middle, and last names, and then display their first, middle, and last initials. For example, if the user enters John William Smith the program should display j. W. S. Write a program that asks the user to enter a series of single-digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string. For example, if the user enters 2514, the method should return 12, which is...

A Nested Decision Structure Can Be Used To Test More Than One Condition

Nested Decision Structures

The flowchart in the figure starts with a sequence structure. Assuming you have an outdoor thermometer in your window, the first step is Go to the window, and the next step is Read thermometer. A decision structure appears next, testing the condition Cold outside. If this is true, the action wear a coat is performed. Another sequence structure appears next. The step Open the door is performed, followed by Go outside. Quite often, structures must be nested inside other structures. For example,...

Adding and Displaying Records

Midnight Coffee Roasters, Inc. is a small company that imports raw coffee beans from around the world and roasts them to create a variety of gourmet coffees. Julie, the owner of the company, has asked you to write a series of programs that she can use to manage her inventory. After speaking with her, you have determined that a file is needed to keep inventory records. Each record should have two fields to hold the following data Description a string containing the name of the coffee Quantity in...

Info Agx

Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall...

Boolean Expressions and Relational Operators

As previously mentioned, the i f statement tests an expression to determine whether it is true or false. The expressions that are tested by the if statement are called Boolean expressions, named in honor of the English mathematician George Boole. In the 1800s Boole invented a system of mathematics in which the abstract concepts of true and false can be used in computations. Typically, the Boolean expression that is tested by an if statement is formed with a relational operator. A relational...

F Upx

In this chapter you saw an example of how to write an algorithm that determines whether a number is even or odd. Write a program that generates 100 random numbers, and keeps a counr of how many of those random numbers are even and how many are odd. A prime number is a number that is only evenly divisible by itself and 1. For example, rhe number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is nor prime because it can be divided evenly by 1, 2, 3, and 6....

Info Wbm

3 The Coin class simulates a coin that can The__init__method initializes the sideup data attribute with 'Heads'. The toss method generates a random number in the range of 0 through 1. If the number is 0, then sideup is set to 'Heads'. Otherwise, sideup is set to 'Tails'. The get-sideup method returns the value def get-sideup self return self.sideup In line 1 we import the random module. This is necessary because w use the randint function to generate a random number. Line 6 is the beginning of...

Info Dtg

Program Output with input shown in bold Enter the substance's Celsius temperature 104.7 Enter The temperature is too high. Turn the thermostat down and wait 5 minutes. Take the temperature again and enter it. Enter the new Celsius temperature 103.2 Enter The temperature is too high. Turn the thermostat down and wait 5 minutes. Take the temperature again and enter it. Enter the new Celsius temperature 102.1 Enter The temperature is acceptable. Check it again in 15 minutes. Program Output with...

Checking Numeric Ranges with Logical Operators

Sometimes you will need to design an algorithm that determines whether a numeric value is within a specific range of values or outside a specific range of values. When determining whether a number is inside a range, it is best to use the and operator. For example, the following i f statement checks the value in x to determine whether it is in the range of 20 through 40 print 'The value is in the acceptable range. 1 The compound Boolean expression being tested by this statement will be true only...

F F F F F F F F F

Assume the variables a 2, b 4, and c 6. Circle the T or F for each of the following conditions to indicate whether its value is true or false. Explain how short-circuit evaluation works with the and and o r operators. Write an i f statement that displays the message The number is valid if the value referenced by speed is within the range 0 through 200. Write an i f statement that displays the message The number is not valid if the value referenced by speed is outside the range 0 through 200....

Info Xrw

Create the widgets for the top frame. Create the widgets for the top frame. text 'Enter a distance in kilometers ' text 'Enter a distance in kilometers ' Create the widgets for the middle frame.

Please See Reverse Side For Instructions To Open

Python Tutorial

Starting Out with Python introduces students to the basics of programming and prepares them to go on to more complicated languages. With the knowledge acquired by using Python, students gain confidence in their skills and acquire the logic necessary for developing high-quality programs. This book discusses control structures, functions, lists, and file I O before introducing classes. As with all Gaddis texts, clear and easy-to-read code listings, concise and practical real-world examples,...

X Ire

The area of a rectangle is the rectangle's length times its width. Write a program that asks for the length and width of two rectangles. The program should tell the user which rectangle has the greater area, or if the areas are the same. Scientists measure an object's mass in kilograms and its weight in newtons. If you know rhe amount of mass of an object in kilograms, you can calculate its weight in newtons with the following formula Write a program that asks the user to enter an object's...

Modularizing with Functions

10,000-14,999 15,000-17,999 18,000-21,999 22,000 or more pay sales X commission rate advanced pay Program 6-8 shows the code, which is written using several functions. Rather than presenting the entire program at once, let's first examine the main function and then each function separately. Here is the main function Program 6-8 commission_rate.py main function 1 This program calculates a salesperson's pay

Info Vdf

11 print 'You will read', per-month, 'books per month. Program Output with input shown in bold How many books do you want to read 20 Enter How many months will it take 8 Enter You will read 2.5 books per month. In line 8 the expression float books converts the value referenced by books to a float. This ensures that when the division takes place, one of the operands will be a float, thus preventing integer division. WARN ING Notice that in line 8 of Program 2-19, we did not put the entire...

Review Questions Kyk

1. This is a prewritten function that is built into a programming language. 2. This term describes any mechanism that accepts input, performs some operation that cannot be seen on the input, and produces output. 3. This standard library function returns a random integer within a specified range of values. 4. This standard library function returns a random floating-point number in the range of 0.0 up to 1.0 but not including 1.0 . 5. This standard library function returns a random floating-point...

L jL JL

6. Rewrite the following statements using augmented assignment operators. 7. Write a set of nested loops that display 10 rows of characters. There should be 15 characters in each row. 8. Write code that prompts the user to enter a positive nonzero number and validates the input. 9. Write code that prompts the user to enter a number in the range of 1 through 100 and validates the input.

In the Spotlight Jka

Julie has been using the first two programs that you wrote for her. She now has several records stored in the coffee.txt file, and has asked you to write another program that she can use to search for records. She wants to be able to enter a description and see a list of all the records matching that description. Program 7-17 shows the code for the program. Program 7-17 search_coffee_records.py 1 This program allows the user to search the 2 coffee.txt file for records matching a

Info Fpc

Label - Label - Label- Label- - Suttr the store for test ti -Enter the store for test Erter the store for test 3 By examining the sketch we can make a list of the widgets that we need. As we make the list, we will include a brief description of each widget and a name that we will assign to each widget when we construct it. 1 Label Instructs the user to enter the score for test 1. 2 Label Instructs the user to enter the score for test 2. 3 Label Instructs the user to enter the score for test 3....

Programming Exercises Lgm

One foot equals 12 inches. Write a function named f eet_to_inches that accepts a number of feet as an argument, and returns the number of inches in that many feet. Use the function in a program that prompts the user to enter a number of feet and then displays the number of inches in that many feet. Write a program that gives simple math quizzes. The program should display two random numbers that are to be added, such as 247 129 The program should allow the student to enter the answer. If the...

In the Spotlight Atp

Designing a Count-Controlled Loop with the for Statement Your friend Amanda just inherited a European sports car from her uncle. Amanda lives in the United States, and she is afraid she will get a speeding ticket because the car's speedometer indicates kilometers per hour. She has asked you to write a program that displays a table ir values convertec . re- mile per hnuv. The ioLimiia for converting kilometers per hour to miles per hour is In the formula, MPH is the speed in miles per hour and...

Info Cfw

print 'This program calculates the sum of' print 'five numbers you will enter.' Get five numbers and accumulate them, for counter in range 5 number input 'Enter a number ' total total number Display the total of the numbers, print 'The total is', total added to the total variable. When the loop finishes, the total variable will hold the sum of all the numbers that were added to it. This value is displayed in line 18. The Aug men tec .Assign men t ii ratcirs Quite often, programs have assignment...

Programming Exercises Hqb

A bug collector collects bugs every day for seven days. Write a program that keeps a running total of the number of bugs collected during the seven days. The loop should ask for the number of bugs collected for each day, and when the loop is finished, the program should display the total number of bugs collected. Running on a particular treadmill you burn 3.9 calories per minute. Write a program that uses a loop to display the number of calories burned after 10, 15, 20, 25, and 30 minutes....

Info Ibz

Program Output with input shown in bold Enter your first name Matt Enter Enter your last name Hoyle Enter Your name reversed is Hoyle Matt This program demonstrates passing two strings as keyword arguments to a function. first-name raw_input 'Enter your first name ' last-name raw_input 'Enter your last name ' print 'Your name reversed is' reverse_name last last_name, first first_name def reverse_name first, last print last, first Call the main function. main Mixing Keyword Arguments with...