Instantly share code, notes, and snippets.

@acmarsnik

acmarsnik / py4e-pfe-3_2-computePayV3.py

  • Download ZIP
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.
  • Learn more about clone URLs
  • Save acmarsnik/a3983c2bb5cb52a655c6c337ab92e897 to your computer and use it in GitHub Desktop.

@acmarsnik

acmarsnik commented Nov 19, 2017

Write a program to prompt the user for hours and rate per hour to compute gross pay.

Sorry, something went wrong.

acmarsnik commented Nov 21, 2017

Desired Prompt 1: Enter Hours: Example Input 1: 35 Desired Prompt 2: Enter Rate: Example Input 2: 2.75 Desired Output: Pay: 96.25

We won't worry about making sure our pay has exactly two digits after the decimal place for now. If you want, you can play with the built-in Python round function to properly round the resulting pay to two decimal places.

Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.

Rewrite your pay program using try and except so that your program handles non-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program:

Enter Hours: 20 Enter Rate: nine Error, please enter numeric input

Enter Hours: forty Error, please enter numeric input

assignment 3.2 python for everybody

  • Table of Contents
  • Course Home
  • Assignments
  • Peer Instruction (Instructor)
  • Peer Instruction (Student)
  • Change Course
  • Instructor's Page
  • Progress Page
  • Edit Profile
  • Change Password
  • Scratch ActiveCode
  • Scratch Activecode
  • Instructors Guide
  • About Runestone
  • Report A Problem
  • 3.1 How to be a Successful Programmer
  • 3.2 How to Avoid Debugging
  • 3.3 Beginning tips for Debugging
  • 3.4 Know Your Error Messages
  • 3.5 Summary
  • 3.6 Exercises
  • 3.5. Summary" data-toggle="tooltip">
  • 4. Conditional Execution' data-toggle="tooltip" >

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

3.6. Exercises ¶

This page is intentionally blank (for now)

Variables, expressions, and statements

Values and types.

A value is one of the basic things a program works with, like a letter or a number. The values we have seen so far are 1, 2, and “Hello, World!”

These values belong to different types : 2 is an integer, and “Hello, World!” is a string , so called because it contains a “string” of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks.

The print statement also works for integers. We use the python command to start the interpreter.

If you are not sure what type a value has, the interpreter can tell you.

Not surprisingly, strings belong to the type str and integers belong to the type int . Less obviously, numbers with a decimal point belong to a type called float , because these numbers are represented in a format called floating point .

What about values like “17” and “3.2”? They look like numbers, but they are in quotation marks like strings.

They’re strings.

When you type a large integer, you might be tempted to use commas between groups of three digits, as in 1,000,000. This is not a legal integer in Python, but it is legal:

Well, that’s not what we expected at all! Python interprets 1,000,000 as a comma-separated sequence of integers, which it prints with spaces between.

This is the first example we have seen of a semantic error: the code runs without producing an error message, but it doesn’t do the “right” thing.

One of the most powerful features of a programming language is the ability to manipulate variables . A variable is a name that refers to a value.

An assignment statement creates new variables and gives them values:

This example makes three assignments. The first assigns a string to a new variable named message ; the second assigns the integer 17 to n ; the third assigns the (approximate) value of π to pi .

To display the value of a variable, you can use a print statement:

The type of a variable is the type of the value it refers to.

Variable names and keywords

Programmers generally choose names for their variables that are meaningful and document what the variable is used for.

Variable names can be arbitrarily long. They can contain both letters and numbers, but they cannot start with a number. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter (you’ll see why later).

The underscore character ( _ ) can appear in a name. It is often used in names with multiple words, such as my_name or airspeed_of_unladen_swallow . Variable names can start with an underscore character, but we generally avoid doing this unless we are writing library code for others to use.

If you give a variable an illegal name, you get a syntax error:

76trombones is illegal because it begins with a number. more@ is illegal because it contains an illegal character, @. But what’s wrong with class ?

It turns out that class is one of Python’s keywords . The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names.

Python reserves 35 keywords:

You might want to keep this list handy. If the interpreter complains about one of your variable names and you don’t know why, see if it is on this list.

A statement is a unit of code that the Python interpreter can execute. We have seen two kinds of statements: print being an expression statement and assignment.

When you type a statement in interactive mode, the interpreter executes it and displays the result, if there is one.

A script usually contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.

For example, the script

produces the output

The assignment statement produces no output.

Operators and operands

Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands .

The operators + , - , * , / , and ** perform addition, subtraction, multiplication, division, and exponentiation, as in the following examples:

There has been a change in the division operator between Python 2 and Python 3. In Python 3, the result of this division is a floating point result:

The division operator in Python 2 would divide two integers and truncate the result to an integer:

To obtain the same answer in Python 3 use floored ( // integer) division.

In Python 3 integer division functions much more as you would expect if you entered the expression on a calculator.

Expressions

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x has been assigned a value):

If you type an expression in interactive mode, the interpreter evaluates it and displays the result:

But in a script, an expression all by itself doesn’t do anything! This is a common source of confusion for beginners.

Exercise 1: Type the following statements in the Python interpreter to see what they do:

Order of operations

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence . For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way to remember the rules:

P arentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60 , even if it doesn’t change the result.

E xponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3, not 27.

M ultiplication and D ivision have the same precedence, which is higher than A ddition and S ubtraction, which also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.

Operators with the same precedence are evaluated from left to right. So the expression 5-3-1 is 1, not 3, because the 5-3 happens first and then 1 is subtracted from 2.

When in doubt, always put parentheses in your expressions to make sure the computations are performed in the order you intend.

Modulus operator

The modulus operator works on integers and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign ( % ). The syntax is the same as for other operators:

So 7 divided by 3 is 2 with 1 left over.

The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another: if x % y is zero, then x is divisible by y .

You can also extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly, x % 100 yields the last two digits.

String operations

The + operator works with strings, but it is not addition in the mathematical sense. Instead it performs concatenation , which means joining the strings by linking them end to end. For example:

The * operator also works with strings by multiplying the content of a string by an integer. For example:

Asking the user for input

Sometimes we would like to take the value for a variable from the user via their keyboard. Python provides a built-in function called input that gets input from the keyboard 1 . When this function is called, the program stops and waits for the user to type something. When the user presses Return or Enter , the program resumes and input returns what the user typed as a string.

Before getting input from the user, it is a good idea to print a prompt telling the user what to input. You can pass a string to input to be displayed to the user before pausing for input:

The sequence \n at the end of the prompt represents a newline , which is a special character that causes a line break. That’s why the user’s input appears below the prompt.

If you expect the user to type an integer, you can try to convert the return value to int using the int() function:

But if the user types something other than a string of digits, you get an error:

We will see how to handle this kind of error later.

As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why.

For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments , and in Python they start with the # symbol:

In this case, the comment appears on a line by itself. You can also put comments at the end of a line:

Everything from the # to the end of the line is ignored; it has no effect on the program.

Comments are most useful when they document non-obvious features of the code. It is reasonable to assume that the reader can figure out what the code does; it is much more useful to explain why .

This comment is redundant with the code and useless:

This comment contains useful information that is not in the code:

Good variable names can reduce the need for comments, but long names can make complex expressions hard to read, so there is a trade-off.

Choosing mnemonic variable names

As long as you follow the simple rules of variable naming, and avoid reserved words, you have a lot of choice when you name your variables. In the beginning, this choice can be confusing both when you read a program and when you write your own programs. For example, the following three programs are identical in terms of what they accomplish, but very different when you read them and try to understand them.

The Python interpreter sees all three of these programs as exactly the same but humans see and understand these programs quite differently. Humans will most quickly understand the intent of the second program because the programmer has chosen variable names that reflect their intent regarding what data will be stored in each variable.

We call these wisely chosen variable names “mnemonic variable names”. The word mnemonic 2 means “memory aid”. We choose mnemonic variable names to help us remember why we created the variable in the first place.

While this all sounds great, and it is a very good idea to use mnemonic variable names, mnemonic variable names can get in the way of a beginning programmer’s ability to parse and understand code. This is because beginning programmers have not yet memorized the reserved words (there are only 35 of them) and sometimes variables with names that are too descriptive start to look like part of the language and not just well-chosen variable names.

Take a quick look at the following Python sample code which loops through some data. We will cover loops soon, but for now try to just puzzle through what this means:

What is happening here? Which of the tokens (for, word, in, etc.) are reserved words and which are just variable names? Does Python understand at a fundamental level the notion of words? Beginning programmers have trouble separating what parts of the code must be the same as this example and what parts of the code are simply choices made by the programmer.

The following code is equivalent to the above code:

It is easier for the beginning programmer to look at this code and know which parts are reserved words defined by Python and which parts are simply variable names chosen by the programmer. It is pretty clear that Python has no fundamental understanding of pizza and slices and the fact that a pizza consists of a set of one or more slices.

But if our program is truly about reading data and looking for words in the data, pizza and slice are very un-mnemonic variable names. Choosing them as variable names distracts from the meaning of the program.

After a pretty short period of time, you will know the most common reserved words and you will start to see the reserved words jumping out at you:

The parts of the code that are defined by Python ( for , in , print , and : ) are in bold and the programmer-chosen variables ( word and words ) are not in bold. Many text editors are aware of Python syntax and will color reserved words differently to give you clues to keep your variables and reserved words separate. After a while you will begin to read Python and quickly determine what is a variable and what is a reserved word.

At this point, the syntax error you are most likely to make is an illegal variable name, like class and yield , which are keywords, or odd~job and US$ , which contain illegal characters.

If you put a space in a variable name, Python thinks it is two operands without an operator:

For syntax errors, the error messages don’t help much. The most common messages are SyntaxError: invalid syntax which is not very informative.

The runtime error you are most likely to make is a “use before def;” that is, trying to use a variable before you have assigned a value. This can happen if you spell a variable name wrong:

Variables names are case sensitive, so LaTeX is not the same as latex .

At this point, the most likely cause of a semantic error is the order of operations. For example, to evaluate 1/2 π , you might be tempted to write

But the division happens first, so you would get π /2 , which is not the same thing! There is no way for Python to know what you meant to write, so in this case you don’t get an error message; you just get the wrong answer.

Exercise 2: Write a program that uses input to prompt a user for their name and then welcomes them.

Exercise 3: Write a program to prompt the user for hours and rate per hour to compute gross pay.

We won’t worry about making sure our pay has exactly two digits after the decimal place for now. If you want, you can play with the built-in Python round function to properly round the resulting pay to two decimal places.

Exercise 4: Assume that we execute the following assignment statements:

For each of the following expressions, write the value of the expression and the type (of the value of the expression).

Use the Python interpreter to check your answers.

Exercise 5: Write a program which prompts the user for a Celsius temperature, convert the temperature to Fahrenheit, and print out the converted temperature.

If you find a mistake in this book, feel free to send me a fix using Github .

assignment 3.2 python for everybody

Assignment 3.3 | Week-5 | Programming for Everybody (Getting Started with Python) By Coursera

Assignment 3.3 | Week-5 | Programming for Everybody (Getting Started with Python) By Coursera

Coursera Programming for Everybody (Getting Started with Python) Week 5  Assignment 3.3 

 Question:    3.3  Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:

Score Grade

>= 0.9 A

>= 0.8 B

>= 0.7 C

>= 0.6 D

If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.

Assignment 3.3 | Week-5 | Programming for Everybody (Getting Started with Python) By Coursera

Do Not Only Use These Quizzes For Getting Certificates.You Can Take Help From These Quizzes Answer. All Quizzes & Contents Are Free Of Charge. ✅ If You Want Any Quiz Answers Then Please  Contact Us

Related Questions & Answers:

  • Programming for Everybody (Getting Started with Python) – Coursera Quiz Answers Programming for Everybody (Getting Started with Python) – Coursera 4.8 Stars (167,402 ratings)   Instructor: Charles Russell Severance Enroll Now   This Programming ... Read more...
  • Assignment 2.2 | Week-4 | Programming for Everybody (Getting Started with Python) By Coursera   Coursera Programming for Everybody (Getting Started with Python) Week 4  Assignment 2.2   Question:  2.2 Write a program that uses input to prompt ... Read more...
  • Chapter 5 (Quiz Answers) | Week-7 | Programming for Everybody (Getting Started with Python) By Coursera Coursera Programming for Everybody (Getting Started with Python) Week 7 Chapter 5 Graded Quiz • 30 min 1. What is ... Read more...
  • Chapter 4 (Quiz Answers) | Week-6 | Programming for Everybody (Getting Started with Python) By Coursera Coursera Programming for Everybody (Getting Started with Python) Week 6 Chapter 4 Graded Quiz • 30 min 1. Which Python ... Read more...
  • Chapter 3 (Quiz Answers) | Week-5 | Programming for Everybody (Getting Started with Python) By Coursera Coursera Programming for Everybody (Getting Started with Python) Week 5 Chapter 3 Graded Quiz • 30 min 1. What do ... Read more...
  • Chapter 2 (Quiz Answers) | Week-4 | Programming for Everybody (Getting Started with Python) By Coursera Coursera Programming for Everybody (Getting Started with Python) Week 4 Chapter 2 Graded Quiz • 30 min 1. Which of ... Read more...

Leave a Comment Cancel reply

Save my name, email, and website in this browser for the next time I comment.

Please Enable JavaScript in your Browser to Visit this Site.

assignment 3.2 python for everybody

IMAGES

  1. Coursera Assignment 2.3 Solved Python for Everybody

    assignment 3.2 python for everybody

  2. Coursera

    assignment 3.2 python for everybody

  3. Python for everybody assignment 3.3 Ans || programming for everybody Coursera assignment 3.3 program

    assignment 3.2 python for everybody

  4. Python For Everybody (All the Solved Exercises

    assignment 3.2 python for everybody

  5. Programming for Everybody (Getting Started with Python)

    assignment 3.2 python for everybody

  6. I Reviewed the Python For Everybody Specialization on Coursera by University of Michigan

    assignment 3.2 python for everybody

VIDEO

  1. Python Assignment Operator #coding #assignmentoperators #phython

  2. Python Assignment 2

  3. Python Week 1 Graded Assignment(IITM)

  4. python assignment operator

  5. Python Assignment 2

  6. iitm python Week 3 Graded Assignment solutions

COMMENTS

  1. Python For Everybody Assignment 3.2 program solution

    Coursera: Python For Everybody Assignment 3.2 program solution | Assignment 3.2 Python For EverybodyCode link: https://drive.google.com/file/d/1zBODlCGxzdWBr...

  2. GitHub

    This contains all the practices for the lectures, custom answers to the assignments and additional inline notes for "Python for Everybody Specialization" on Coursera by the University of Michigan. 41 stars 54 forks Branches Tags Activity

  3. sersavn/coursera-python-for-everybody-specialization

    Current repository contains all assignments, notes, quizzes and course materials from the "Python for Everybody Specialization" provided by Coursera and University of Michigan. 139 stars 80 forks Branches Tags Activity

  4. python-for-everybody-solutions/exercise3_2.py at master

    Solutions to Python for Everybody: Exploring Data using Python 3 by Charles Severance - jmelahman/python-for-everybody-solutions

  5. Exercise 3.2

    Raw. Exercise 3.2 - Python for Everybody. Rewrite your pay program using try and except so that your. program handles non-numeric input gracefully by printing a message. and exiting the program. The following shows two executions of the. program: Enter Hours: 20.

  6. Python for everybody assignment 3.3

    Python for everybody assignment 3.3. Ask Question Asked 1 year, 11 months ago. Modified 1 year, 11 months ago. Viewed 610 times ... Not sure what is wrong with this code for Python for Everybody 5.2. Hot Network Questions Zsh: How to workaround "no matches found" issue

  7. Python For Everybody Specialization, Programming for Everybody ...

    Python For Everybody Specialization, Programming for Everybody (Getting Started with Python), Assignment 3.2: Compute Pay V3 - py4e-pfe-3_2-computePayV3.py

  8. Programming for Everybody (Getting Started with Python)

    There are 7 modules in this course. This course aims to teach everyone the basics of programming computers using Python. We cover the basics of how one constructs a program from a series of simple instructions in Python. The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should ...

  9. Python for Everybody

    Python for Everybody - Interactive¶ Assignments¶ Assignments; Table of Contents ...

  10. Python for Everyone

    Exercise 28. At Quizlet, we're giving you the tools you need to take on any subject without having to carry around solutions manuals or printing out PDFs! Now, with expert-verified solutions from Python for Everyone 2nd Edition, you'll learn how to solve your toughest homework problems. Our resource for Python for Everyone includes answers ...

  11. PY4E

    Coursera: Python for Everybody Specialization; edX: Python for Everybody; FreeCodeCamp; Free certificates for University of Michigan students and staff; If you log in to this site you have joined a free, global open and online course. You have a grade book, autograded assignments, discussion forums, and can earn badges for your efforts.

  12. python-for-everybody/wk2

    python-for-everybody. /. wk2 - assignment 2.3.py. Cannot retrieve latest commit at this time. History. Code. 20 lines (11 loc) · 517 Bytes. # 2.3 Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay. # Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25).

  13. Coursera: Python For Everybody Assignment 3.3 Solution

    Coursera: Programming For Everybody Assignment 3.3 program solution Answer | Python for Everybody Assignment 3.3 program solution.If your Code is Working Buy...

  14. Python For Everyone, 3rd Edition

    Python for Everyone, 3rd Edition is an introduction to programming designed to serve a wide range of student interests and abilities, focused on the essentials, and on effective learning. ... ST 2 Combining Assignment and Arithmetic 38. ST 3 Line Joining 38. 2.3 PROBLEM SOLVING: First Do It By Hand 39. WE 1 Computing Travel Time 40. 2.4 Strings 41.

  15. Python For Everyone, 3rd Edition

    Python for Everyone, 3rd Edition is an introduction to programming designed to serve a wide range of student interests and abilities, focused on the essentials, and on effective learning. ... ST 2 Combining Assignment and Arithmetic 38. ST 3 Line Joining 38. 2.3 PROBLEM SOLVING: First Do It By Hand 39. WE 1 Computing Travel Time 40. 2.4 Strings 41.

  16. Programming for Everybody (Getting Started with Python)

    There are 7 modules in this course. This course aims to teach everyone the basics of programming computers using Python. We cover the basics of how one constructs a program from a series of simple instructions in Python. The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should ...

  17. Programming for Everybody Assignments Complete Course Solved ...

    0:00 Intro1:24 Assignment 2.22:24 Assignment 2.34:09 Assignment 3.16:13 Assignment 3.38:26 Assignment 4.610:30 Assignment 5.2Programming for Everybody (Getti...

  18. 3.6. Exercises

    3.5 Summary. 3.6 Exercises. Before you keep reading... Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

  19. jmelahman/python-for-everybody-solutions

    Solutions to Python for Everybody: Exploring Data using Python 3 by Charles Severance - jmelahman/python-for-everybody-solutions

  20. PY4E

    The division operator in Python 2 would divide two integers and truncate the result to an integer: >>> minute = 59 >>> minute/60 0. To obtain the same answer in Python 3 use floored ( // integer) division. >>> minute = 59 >>> minute//60 0. In Python 3 integer division functions much more as you would expect if you entered the expression on a ...

  21. Programming for Everybody (Getting Started with Python) Course by

    This mini-course is intended to for you to demonstrate foundational Python skills for working with data. This course primarily involves completing a project in which you will assume the role of a Data Scientist or a Data Analyst and be provided with a real-world data set and a real-world inspired scenario to identify patterns and trends.

  22. Python for Everybody Chapter 2 Flashcards

    Python for Everyone Chapter 4. 14 terms. Sydney_Singleton2. Preview. 1.2.1 Systems Software. 60 terms. LLL2222. Preview. rts 7 part 1 . 10 terms. mcpatdaphat. Preview. Terms in this set (36) What's a string? ... An assignment statement. creates new variables and gives them values: ...

  23. Assignment solutions for python for everybody

    Python 100.0%. Assignment solutions for python for everybody. Contribute to sweehors/python-for-everybody development by creating an account on GitHub.

  24. Assignment 3.3

    The above questions are from " Programming for Everybody (Getting Started with Python) " You can discover all the refreshed questions and answers related to this on the " Programming for Everybody (Getting Started with Python) By Coursera " page. If you find the updated questions or answers, do comment on this page and let us know. We will update the answers as soon as possible.