-4

I am working on a homework problem for an introductory level Python class and am having difficulty understanding functions involving defining multiple parameters.

I have already tried numerous attempts from a variety of online resources without any luck.

The question asks "Write the definition of a function typing_speed, that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float)."

This is the code that is displaying an incorrect expression.

def typing_speed(num_words,time_interval):
    num_words>=0
    time_interval>0
    result=float((num_words)/(time_interval*60))
    return result

Thank you for any help

  • 1
    What do you mean by "displaying an incorrect expression"? You haven't included that result in your posting. – Prune Aug 27 '19 at 23:01

4 Answers4

2

Two things: First off, the first two lines in your function don't actually do anything.

num_words>=0
time_interval>0

both evaluate to True but are not assigned to anything. If you want them to perform input validation, you have to use them as the condition in an if, like so:

if num_words >= 0:
    # do what you want
else:
    # the input is wrong. maybe throw an exception or return 0?

With what you actually want to do, you are on the right track. However, your math equation is wrong: You want to convert the seconds to minutes. One minute has 60 seconds, so divide the seconds by 60 and you get the correct result ;)

Taxel
  • 797
  • 7
  • 22
0

you need to remove

num_words>=0
time_interval>0

as the parameters in the function definition i.e. num_words and interval already store the values.

def typing_speed(num_words,time_interval):
    result=float((num_words)/(time_interval*60))
    return result

And always add the error to the code as well.

If you want to add value checking such that time and number of words are positive or not Best way to check function arguments in Python

aunsid
  • 343
  • 1
  • 10
0

num_words and time_interval should be in if statement. Like:

def typing_speed(num_words,time_interval):
   if (num_words>=0 and time_interval>0):
       result=float((num_words)/(time_interval*60))
       return result
   else:
        #handle a case if inputs are not proper. Print error for example.
R. Arctor
  • 618
  • 3
  • 13
0

I suspect that the problem of "displaying an incorrect expression" is supposed to mean that you got the wrong answer. Your computation is incorrect. Try

    result = float(num_words / (time_interval / 60) )

With the updated code -- without the Boolean expressions that aren't part of the posted problem statement:

def typing_speed(num_words,time_interval):
    result = num_words / (time_interval / 60)
    return result

print(typing_speed(25, 30))

Output:

50.0
Prune
  • 72,213
  • 14
  • 48
  • 72