Jake
Jake

Reputation: 11

Step Counter Python Lab

I'm trying to solve this programming problem in Python:

A pedometer treats walking 1 step as walking 2.5 feet. Define a function named feet_to_steps that takes a float as a parameter, representing the number of feet walked, and returns an integer that represents the number of steps walked.

Then, write a main program that reads the number of feet walked as an input, calls function feet_to_steps() with the input as an argument, and outputs the number of steps. Use floating-point arithmetic to perform the conversion.

Ex: If the input is: 150.5

the output is: 60

This is my code so far:

def feet_to_steps(user_feet):
    steps_walked = user_feet / 2.5
    return steps_walked
    
    
if __name__ == '__main__':
    
    input_feet = float(input())
    steps_walked = feet_to_steps(input_feet)
    print(int(steps_walked))

Two of my test cases passed where the input was 150.5 with an output of 60, and another with an input of 10000 with an output of 4000. The other two failed saying this:

I know it's the second part of the prompt that is messing me up, but I can't figure out how to make the two different conditions work together with the same function. I also don't understand the if__name__=='main': part at all either. It was just included in the default template.

Upvotes: 0

Views: 20026

Answers (3)

John B.
John B.

Reputation: 53

Now, I do not know what the correct inputs would be for the two that you missed because there are different types of division in Python that could dictate more than one "correct" answer based on your guidelines and/or expected output. However, because someone already explained whole/floor division (using // instead of /) and the directions say to use "floating point arithmetic", I will provide another means of rounding off a float into an integer.

To start, I want to explain what happens in your example that may be causing the issues. When you data cast something into an integer such as the following:

print(int(steps_walked))

You are actually rounding down to the nearest integer. So let's say your function produced a return value of 7.9999. If you data cast it like in the example above, you will get 7. Using whole or floor division will do the same thing, with only difference in output being that you will have a trailing ".0" because you are using whole/floor division with floats.

The reason your lab returned float points as an incorrect value may be due to how the lab tests your code. It may strictly be checking the function and may not even be referring to your print statement at the bottom. You can check this by data casting the return statement in the function rather than the print statement in main.

Instead, what you may want to use is the round() function. It rounds floating points up if the decimal point is >= .5 or down if < .5. See below:

def feet_to_steps(user_feet):
    steps_walked = user_feet / 2.5
    return round(steps_walked)
    
    
if __name__ == '__main__':
    
    input_feet = float(input())
    steps_walked = feet_to_steps(input_feet)
    print(steps_walked)

If you use the round() function, you will notice that you get the same output with the first three inputs (150.5, 10000, and 11) if you data cast or use whole/floor division. However, the difference comes up with your last input(79.25). When your division is evaluated to 31.7, using round() will push your quotient to 32 because of the .7. However, data casting and whole/floor division will push your quotient down to 31 and 31.0 respectively.

round() can take one or two arguments. If you use one argument such as the following:

round(31.789)

It will round to the nearest whole number. However if you specify what place you want it rounded to in a second argument such as this:

round(31.789, 1)

You will get 31.8 or 1 place to the right of the decimal.

Again, I do not know what division your lab intended so even though the round() feature is more accurate, your lab may expect your floating points to be rounded down to get full points. If that is the case, at least you learned something new.

Upvotes: -1

Landon Dubey
Landon Dubey

Reputation: 21

Your primary issue is that, by dividing by 2.5, the result is converted to a floating-point (decimal) value. We want the value to be an integer value, so instead of using / to do division, we can use // for floor division, which will produce an integer value from the operation, discarding any fractional value.

def feet_to_steps(user_feet):
    steps_walked = user_feet // 2.5
    return steps_walked
    
    
if __name__ == '__main__':
    
    input_feet = float(input())
    steps_walked = feet_to_steps(input_feet)
    print(int(steps_walked))

Upvotes: 2

ElliotSknr
ElliotSknr

Reputation: 326

Your code is mostly fine, however, you are converting the answer from a float to an int too late in your code (outside of the feet_to_steps function). Try replacing return steps_walked with return int(steps_walked)

Lastly, if__name__=='main': is a way of telling Python to only run the next block of code if it is being run directly (i.e. not imported as a module).

This makes it so if you were to create a second .py file and import this file, the code within the if__name__=='main': block would not run, but you would still be able to access and use the functions defined in the file.

Upvotes: 2

Related Questions