- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
- OverflowAI GenAI features for Teams
- OverflowAPI Train & fine-tune LLMs
- Labs The future of collective knowledge sharing
- About the company Visit the blog
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
Coursera Python: Programming for everybody assignment 5.2
I have been taking Coursera's course, Programming for Everybody with Python. But one of the assignment 5.2 on week 7 got my attention.
The objective is to make the user enter some numbers and enter done, when he entered all the numbers he wanted. After that, the output should be the biggest number and smallest number he entered.
Here is the problem. If I enter a negative number it is not displayed. Let's say I enter: 32, 55,10, -2 76. The output should be 76 and -2. But what really happens is that 76 and 10 are printed out.
Do you guys have any idea why this happens?
Here is the code.
- variable-assignment
- 2 What do you think range(-2) does? – José Sánchez Commented Dec 28, 2016 at 12:33
- 1 Why are you even looping over a range? – TigerhawkT3 Commented Dec 28, 2016 at 12:38
3 Answers 3
Well,the issue is that why are you iterating over an int if it isnt a list? You can rather do it with out a loop:
- The continue isn't necessary, as the only other code in that loop is the elif block which already won't run if the if executes. – TigerhawkT3 Commented Dec 28, 2016 at 12:55
- True,didnt see that. – Taufiq Rahman Commented Dec 28, 2016 at 12:57
Well, this was my answer. Try this. Let me know what you don't understand.
- 2 Welcome to SO. While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please check how-to-answer for more details. – alan.elkin Commented Jun 13, 2020 at 20:58
This code will work for your assignment
largest = None smallest = None while True: try: num = input("Enter a number: ") if num == "done": break # print (num)
print ("Maximum is", largest) print ("Minimum is", smallest)
Not the answer you're looking for? Browse other questions tagged python numbers variable-assignment or ask your own question .
- The Overflow Blog
- Masked self-attention: How LLMs learn relationships between tokens
- Deedy Das: from coding at Meta, to search at Google, to investing with Anthropic
- Featured on Meta
- User activation: Learnings and opportunities
- Preventing unauthorized automated access to the network
- Feedback Requested: How do you use the tagged questions page?
Hot Network Questions
- How to make the content inside a longtable 100% centered both vertically and horizontally?
- Meaning of "break" in horse races
- Taking out the film from the roll can it still work?
- Will a Palm tree in Mars be approximately 2.5 times taller than the same tree on Earth?
- Evil machine/Alien entity kills man but his consciousness/brain remains alive within it, and he spends eons reading its mind to defeat it and escape
- What is the origin of the many extra mnemonics in Manx Software Systems’ 8086 assembler?
- How to enable (turn on) a 5V power rail with a 3.3V MCU power rail?
- What happens when you hide from a creature with Truesight?
- How can I award a player an additional Feat?
- Extended font size options not being applied on macos
- Is this a proof for energy conservation?
- Used car dealership refused to let me use my OBDII on their car, is this a red flag?
- Is it OK to use the same transfer pump for gear oil and engine oil?
- Switch or switches in the context of trains in American English?
- Does any abbreviation especially used in writing?
- Is it considered vulgar to use "massive" to describe female breast size?
- In a shell script, how do I wait for a volume to be available?
- Has mandated/suggested cycling liability insurance been implemented successfully in any jurisdiction?
- How many natural operations on subsets are there?
- Since when is Pennsylvania "midwestern"?
- How to format units inside math environment?
- How do you measure exactly 31 minutes by burning the ropes?
- Oxidation of methanoic acid (formic acid) to carbon dioxide and water
- How to write an Antagonist that is hot, manipulative, but has good reasoning for being the 'villain'?
Instantly share code, notes, and snippets.
jennyonjourney / gist:e4982d3fedd6c70f1da239f86f1918b7
- Download ZIP
- Star ( 4 ) 4 You must be signed in to star a gist
- Fork ( 0 ) 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 jennyonjourney/e4982d3fedd6c70f1da239f86f1918b7 to your computer and use it in GitHub Desktop.
def computepay(h,r): | |
if h<=40: | |
pay=h*r | |
elif h>40: | |
pay=40*r+(h-40)*r*1.5 | |
return(pay) | |
hrs = input("Enter Hours:") | |
h = float(hrs) | |
rate = input("Enter rate:") | |
r = float(rate) | |
p = computepay(h,r) | |
print(p) |
kusumamahesh123 commented Jun 14, 2020
thank you its working
Sorry, something went wrong.
yaswanth67 commented Jun 14, 2020 via email
arun95gangwar commented Jul 3, 2020
hrs = input("Enter Hours:") rate = input("Enter rate:") h=float(hrs) r=float(rate) def computepay(h,r): if h>40: fix=r h extra=(h-40) (r*0.5) pay=fix+extra
total=computepay(h,r) print("Pay",total)
AbhilashaSharma14 commented Jul 24, 2020
def computepay(h,r): if h<=40: pay=h r elif h>40: pay=40 r+(h-40) r 1.5 return(pay)
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate) p = computepay(h,r) print(p)
my-name-arch commented Jul 27, 2020
input("Enter Hours:") h=float(hrs) rate=input("enter rate") r=float(rate) def computepay(h,r): if h<=40: pay=hr elif h>=40: pay=40r+(h-40)1.5r return (pay) p=computepay(h,r): print("Pay",p) I don;t see what is wrong with this code. It comes up as a parse error. Can someone please help
AhmedSaidi99 commented Aug 8, 2020
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate) p = computepay(h,r) print("Pay", p)
MimAhmed commented Aug 12, 2020
hrs = input("Enter Hours:") hour_float = float(hrs) rate = input("Enter rate:") rate_float = float(rate) final_pay = computepay(hour_float ,rate_float) print(final_pay)
HaamzaHM commented Oct 17, 2020
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate) p = computepay(h,r) print("Pay",p)
Japarna commented Oct 20, 2020
hrs= input("enter the hours:") rate=input("enter the rate per hour:") hrs=float(hrs) rate=float(rate) def computepay(hours,rate): if hrs <= 40: pay=hrs
pay=computepay(hrs,rate) print('Pay',pay)
JohnLeMay4 commented Oct 28, 2020
Why isn't this working? It is literally what Chuck did in the video and it works in my command prompt:
def computepay(hours, rate) : #print("In computepay", hours, rate) if hours > 40 : reg = rate * hours otp = (hours - 40.0) * (rate * 0.5) pay = reg + otp else: pay = hours * rate #print("Returning",pay) return pay sh = input("Enter Hours: ") sr = input("Enter Rate: ") fh = float(sh) fr = float(sr) xp = computepay(fh,fr)
print("Pay:",xp)
Why isn't this working? It is literally what Chuck did in the video and it works in my command prompt: def computepay(hours, rate) : #print("In computepay", hours, rate) if hours > 40 : reg = rate * hours otp = (hours - 40.0) * (rate * 0.5) pay = reg + otp else: pay = hours * rate #print("Returning",pay) return pay sh = input("Enter Hours: ") sr = input("Enter Rate: ") fh = float(sh) fr = float(sr) xp = computepay(fh,fr) print("Pay:",xp)
IT WAS THE COLON IN THE FINAL PRINT STATEMENT. I AM GOING TO SLEEP. Whew.
sisysl commented Jan 22, 2021
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate) def computepay(h,r): if h<=40: pay=h_r elif h>40: pay=40_r+(h-40)_r_1.5 return(pay) p = computepay(h,r) print('Pay',p)
not working
It is working, try again.
Be careful for indent when use function.
ozguripekci commented Jun 15, 2021
sooryansatheesh commented Jun 21, 2021
#Perfectly Working 💙 #function def computepay(hours, per_rate_hours): #overtime if (hours>40): pay = hours * per_rate_hours overtime = (hours - 40) * (0.5 * per_rate_hours) payment = pay + overtime else: payment = hours * per_rate_hours return payment #code begins hours = input("Enter Hours: ") per_rate_hours = input("Enter Per Rate Hour: ") #try and except try: f_hours= float(hours) f_per_rate_hours=float(per_rate_hours) except: print("Error") quit() final_pay = computepay(f_hours, f_per_rate_hours) print("Pay", final_pay)
You will get an error "You have to prompt for the data"...
def computepay(): hours = float(input("Enter Hours:")) rate_per_hour = float(input("Enter Rate per hour:")) if hours>40: pay=(40 rate_per_hour)+((hours-40) rate_per_hour 1.5) else :pay=(hours rate_per_hour) return pay
print("Pay",computepay()) quit()
my code above gives the exact answer but the autograder rejects it by telling that you must prompt for the data
MuhammadShayan17 commented Jul 19, 2021
it's because of the indent issue in line 6; the return function should be backwards, and with the de-indent apart from the above line.. Then I hope that the above code would have worked just right and fine.. :)
The only thing I find wrong in this programming code is the "indent" thing, as I think, you totally forgot about using indents and all..
Hmm, right, but along with this, I guess, there's also the indent thing issue here.. But nvm, all's good when the end's good I guess.. :/
def computepay(): hours = float(input("Enter Hours:")) rate_per_hour = float(input("Enter Rate per hour:")) if hours>40: pay=(40_rate_per_hour)+((hours-40)_rate_per_hour_1.5) else :pay=(hours_rate_per_hour) return pay print("Pay",computepay()) quit() my code above gives the exact answer but the autograder rejects it by telling that you must prompt for the data
Just look for the indent here, I guess. Apart from this, I think the autograder might also be not scanning and coding the double function you've put and typed out there, so yeaah...
ozguripekci commented Jul 19, 2021
Sometimes, "copy and paste" is not working. You need to write all code one-by-one on your IDE. And sometimes because of the "copy and paste", indent problems come through. Best wishes guys.
iamfusta commented Oct 31, 2021
#fixed TR_iamfusta
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate)
def computepay(h,r): if h<=40: pay=hr elif h>40: pay=40*r+(h-40) r 1.5 return(pay) p = computepay(h,r) print('Pay',p)
dynamodave789 commented Apr 21, 2022
def computepay(h, r): if h <= 40: pay = h * r elif h > 40: pay = (40*r+(h-40) 1.5 r) return(pay)
hrs = input("Enter Hours: ") h = float(hrs) rate = input("Enter Rate: ") r = float(rate) p = computepay(h, r) print("Pay", p)
eddshine commented May 6, 2022 • edited Loading
Here's my code: (Only 9 lines of code plus it's very easy to understand)
Have fun! :)
aliimran-ux commented Jun 16, 2022
This is perfect code 👍
sunnyfisher429 commented Jun 26, 2022
how can i fix this ?
hrs= input("Enter Hours:") rates=input("Enter hours:") h=float(hrs) r=float(rates)
def computepay(hrs,rates) if h>=40: p=(40+(fh-40)) r (fr 1.5) else h<=40: p=h r return(p)
sp=computepay(h,r) print ('Pay',p)
imranlondon commented Sep 17, 2022 • edited Loading
Pay and over time caculater.
def computepay(hours, rates): if hours <= 40: print(hours * rates) else: print ((hours - 40) * (rates * 1.5) + (40 * rates))
If hours are more than 40, the mean user did over time, and the rate differs from the actual rate. So, first, we count extra hours from 40, then multiply with different rates and then the original hours at the regular rate. Then combine both.
Taking input from user.
hours = float(input("Enter hours :")) rates = float(input("Enter rates :"))
called function
computepay(hours,rates)
sbedoyac commented Nov 4, 2022
Thaks for that comment, it was the solution
CristianoFIlho commented Dec 23, 2022
def computepay(hours, rate): if hours <= 40: return hours * rate else: overtime_hours = hours - 40 overtime_pay = overtime_hours * (rate * 1.5) return 40 * rate + overtime_pay
hours = float(input("Enter the number of hours worked: ")) rate = float(input("Enter the rate per hour: ")) gross_pay = computepay(hours, rate) print("Pay %.2f" % gross_pay)
programmarself commented Jun 23, 2023
its works 100%. def computepay(h,r): if h<=40: pay=h r elif h>40: pay=40 r+(h-40) r 1.5 return(pay)
silo3605 commented Jan 19, 2024 • edited Loading
This worked for me perfectly, most of the times it has to do with indentations, or Colons or " " improperly placed. def computepay(h, r): if hours <= 40: pay = hours * rate else: pay = 40 * rate + (hours - 40) * rate * 1.5 return pay
hours = float(input("Enter hours: ")) rate = float(input("Enter rate per hour: "))
p = computepay(10, 20) print("Pay", p)
Note: There are indentations in this code: if, else and returned must be aligned; pay, pay must also be aligned. If using python 3, hit the tab key once, which should place if right between f in def and space and c in computepay(h,r): The first pay should be right beneath hours of the if statement. The second pay should be beneath se of the else: and lastly return pay should be in alignment with else: so it should be if hours, else: and return pay aligned correctly and the same for pay by using the space bar in your pc. There two spaces after lines:6 and 10
Programming for Everybody - Python - Coursera (Module 1)
Get better grades with Learn
82% of students achieve A’s after using Learn
Fundamentals of Database Systems
Information Technology Project Management: Providing Measurable Organizational Value
Words that have predefined meanings that cannot be changed. You cannot use reserved words as variable names/identifiers Choose matching term 1 Reserved Words 2 Assignment Statement 3 continue (reserved word) 4 Variable Name Rules (Python) Don't know?
3000+ Courses from California Community Colleges
3000+ Online Courses from California Community Colleges (Fall 2024)
Class Central experiments with cataloging online courses from California Community Colleges, offering diverse, affordable, and credit-worthy learning opportunities.
- 6 Best Content Marketing Courses for 2024
- 6 Best Microsoft Word Courses for Beginners for 2024
- 10 Best Python Courses for 2024
- [2024] 220+ Math Online Courses from World’s Top Universities
Massive List of MOOC-based Microcredentials
600 Free Google Certifications
Most common
- project management
- web development
Popular subjects
Digital Marketing
Graphic Design
Popular courses
FinTech Foundations and Overview
Understanding Dementia
How to Succeed at: Interviews
Organize and share your learning with Class Central Lists.
View our Lists Showcase
Class Central is learner-supported. When you buy through links on our site, we may earn an affiliate commission.
Programming for Everybody (Getting Started with Python)
University of Michigan via Coursera Help
- Learn How to Sign up to Coursera courses for free
- 1800+ Coursera Courses That Are Still Completely Free
- Chapter One - Why we Program?
- These are the course-wide materials as well as the first part of Chapter One where we explore what it means to write programs. We finished Chapter One and had the quiz and first assignment in the third week of the class. Throughout the course, you may want to come back and look at these materials. This section should not take you an entire week.
- Installing Python
- In this module you will set things up so you can write Python programs. Not all activities in this module are required for this class so please read the "Using Python in this Class" material for details.
- Chapter One: Why We Program (continued)
- In the first chapter, we try to cover the "big picture" of programming so you get a "table of contents" of the rest of the book. Don't worry if not everything makes perfect sense the first time you hear it. This chapter is quite broad and you would benefit from reading the chapter in the book in addition to watching the lectures to help it all sink in. You might want to come back and re-watch these lectures after you have finished a few more chapters.
- Chapter Two: Variables and Expressions
- In this chapter we cover how a program uses the computer's memory to store, retrieve and calculate information.
- Chapter Three: Conditional Code
- In this section we move from sequential code that simply runs one line of code after another to conditional code where some steps are skipped. It is a very simple concept - but it is how computer software makes "choices".
- Chapter Four: Functions
- This is a relatively short chapter. We will learn about what functions are and how we can use them. The programs in the first chapters of the book are not large enough to require us to develop functions, but as the book moves into more and more complex programs, functions will be an essential way for us to make sense of our code.
- Chapter Five: Loops and Iteration
- Loops and iteration complete our four basic programming patterns. Loops are the way we tell Python to do something over and over. Loops are the way we build programs that stay with a problem until the problem is solved.
Charles Severance
- programming core
- united states
Never Stop Learning.
Get personalized course recommendations, track subjects and courses with reminders, and more.
IMAGES
VIDEO
COMMENTS
this contains all the answers to the quizes and asssignments for "Programming for Everybody (Getting Started with Python)" on Coursera by the University of Michigan.
This contains all the answers to the assignments and quizzes for "Programming for Everybody (Getting Started with Python)" on Coursera by the University of Michigan.
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.
5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try,except and put out an appropriate message and ignore the number.
Here you will find all the questions, assignments and quiz answers related to "Programming for Everybody (Getting Started with Python) By Coursera"
Programming for Everybody (Getting Started with Python) | Complete Assignment & Quiz Answers | Coursera by University Of Michigan. This course aims to teach everyone the basics of programming ...
I have been taking Coursera's course, Programming for Everybody with Python. But one of the assignment 5.2 on week 7 got my attention. The objective is to make the user enter some numbers and ente...
Complete Solution (Quiz+Programming) for Coursera Specialization Course: Python for Everybody *Course-1: Programming for Everybody (Getting Started with Pyth...
Programming for Everybody (Getting Started with Python), Week (1-7), All Quiz Answers. Think to make 14.2K subscribers Subscribed 423 40K views 4 years ago coursera
Python for everybody - Assignment 4.6. GitHub Gist: instantly share code, notes, and snippets.
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.
Programming for Everybody - Python - Coursera (Module 1) Get a hint. Main Memory. fast small temporary storage - lost on reboot - aka RAM. Where Secondary Memory (aka Storage) sends program to be read, which then sends to CPU. 1 / 38.
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.
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.
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.
Order the book on Amazon: https://amzn.to/3huCub6If you want to support the channel, any donation in PayPal helps: https://bit.ly/2Ss5i90Follow me on Faceboo...
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.
Current repository contains all assignments, notes, quizzes and course materials from the "Python for Everybody Specialization" provided by Coursera and University of Michigan.
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.
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.