[Tutor] Question regarding Python loops

Conner Crowe connerjoy1994 at hotmail.com
Thu Apr 10 04:04:10 CEST 2014


To whom this may concern:

I am struggling on two questions:
Problem 2.
Suppose you are given a function leave(minute) which returns the number of students
that leave an exam during its mth minute. Write a function already_left(t) that
returns the number of students that left at or before minute t of the exam.

Example code:
# To simplify testing, you may wish to define a stand-in function for leave that
# behaves predictably
# Here's one possible stand-in:
def leave(minute):
     if minute > 0:
          return minute
     return 0

def already_left(t):
     # Your code goes here

Example usage:
print already_left(1)
print already_left(4)

Example output:
1
10

So far for this i have:
def leave(minute):
    if minute > 0:
        return minute
    elif minute=0:
        return 0
def already_left(i):
    for i in range(0,t):
        leave(i)

print already_left(8)
print already_left(4) 
Problem 3.

Using your already_left function, write a function percent_left(t, l) that
returns the percent of the class that has left at or before minute t, assuming
the exam is l minutes long, and the entire class has left at or before minute l.
Return your result as a float between 0 and 1.

Example code:

def percent_left(t, l):
     # Your code goes here

Example usage:
print percent_left(0, 4)
print percent_left(1, 4)
print percent_left(4, 4)
print percent_left(30, 50)

Example output:
0.0
0.1
1.0
0.364705882353
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140409/d3020935/attachment.html>


More information about the Tutor mailing list