Beginner Python - simple function

In summary: If you change your code to use casts like this: def typing_speed(wordCount, time): return float((wordCount/time)*60.0) then your function will return the correct value of 85.0 words per minute.
  • #1
zeion
466
1

Homework Statement




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 ).

Homework Equations





The Attempt at a Solution



def typing_speed(a, b):
return float((a/b)*60)

Is that right?
 
Technology news on Phys.org
  • #2
When you include code in a post, use [ code] and [ /code] tags (without the extra space). Doing so preserves your indentation, which is especially important in python.
zeion said:

Homework Statement




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 ).

Homework Equations





The Attempt at a Solution


Code:
def typing_speed(a, b):
	return float((a/b)*60)
Is that right?
I don't think so.

First off - your variable names don't give any clue as to what they are to be used for. It's usually a good idea to use names that help you and other readers understand how the variable will be used.
Code:
def typing_speed(wordCount, time):
	return float((wordCount/time)*60)

I'm not very adept at python, but I suspect that it defines two types of division the same way that C, C++, Java, and other programming languages do. One type of division is integer division, in which the dividend and divisor are both one of the integral types, and in which the quotient is also an integer. With this type of division, 6/2 == 3 (which you would expect), but 6/4 == 1 (which you might not expect.

The other type of division is floating point division, which behaves as you would expect.

Let's suppose that someone types 170 words in 2 minutes (120 seconds). Your function will evaluate 170/120 * 60. It first evaluates 170/120 as 1, with no fractional part. It then multiplies 1 by 60 to get 60, and then casts that result to a float value, 60.0. This is not the right answer, since the correct result is actually 85.0 words per minute.

To fix your code you need to cast one or both operands of the division as a float or double. You are using a cast to convert a/b * 60 to a float, but you are doing it too late. The cast operation needs to happen before the division takes place, not after.
 

1. What is a simple function in Python?

A simple function in Python is a block of code that performs a specific task and can be called or executed multiple times within a program. It usually takes in inputs or parameters, performs some operations, and returns an output.

2. How do I define a simple function in Python?

To define a simple function in Python, you can use the "def" keyword followed by the name of the function, a pair of parentheses, and a colon. Any code that is indented after this line will be a part of the function's body. For example:
def add_numbers(a, b):
    return a + b
This function is called "add_numbers" and takes in two parameters, "a" and "b". It performs the operation of adding them together and returns the result.

3. How do I call or use a simple function in Python?

To call or use a simple function in Python, you can simply use the function name followed by the parentheses and any required arguments or inputs. For example, if you have a function called "square" that takes in one parameter, you can call it like this:
result = square(5)
This will return the result of the square function, which is 25, and store it in the variable "result".

4. Can I have more than one return statement in a simple function?

Yes, you can have more than one return statement in a simple function. However, when a return statement is reached, the function will immediately stop executing and return the specified value. This means that if you have multiple return statements, only one will be executed depending on the conditions in your code.

5. How do I use the return statement in a simple function?

To use the return statement in a simple function, you can simply write "return" followed by the value or variable that you want to return. This can be a single value, multiple values separated by commas, or even a variable that holds a value. For example:
def multiply(a, b):
    return a * b
This function will take two parameters, "a" and "b", and return their product as the output.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
3
Views
307
  • Programming and Computer Science
Replies
2
Views
887
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
Back
Top