Beginner Python - simple function

Click For Summary
SUMMARY

The forum discussion centers on defining a Python function named typing_speed that calculates a person's typing speed in words per minute based on the number of words typed and the time interval in seconds. The initial implementation incorrectly uses integer division, leading to inaccurate results. A corrected version of the function uses descriptive variable names and ensures that at least one operand in the division is cast to a float before performing the calculation, thus providing accurate output.

PREREQUISITES
  • Understanding of Python function definitions
  • Familiarity with data types in Python, specifically integers and floats
  • Knowledge of division operations in programming languages
  • Basic experience with variable naming conventions in coding
NEXT STEPS
  • Learn about Python data types and type casting
  • Explore Python's division operators and their behavior
  • Study best practices for naming variables in programming
  • Practice writing and testing Python functions with different parameters
USEFUL FOR

Beginner Python programmers, students learning to code, and anyone interested in improving their understanding of function definitions and arithmetic operations in Python.

zeion
Messages
455
Reaction score
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
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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 31 ·
2
Replies
31
Views
7K