Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Functions and Looping Exception Flow

Nick B.
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Nick B.
Full Stack JavaScript Techdegree Graduate 31,101 Points

Can someone please explain the float() in Python for me please?

import math

def split_check(total, number_of_people): return math.ceil(total / number_of_people)

try: total_due = float(input("What is the total? ")) number_of_people = int(input("How many people? ")) except ValueError: print("Oh no! That's not a valid value. Try again...") except ZeroDivisionError: # else:
amount_due = split_check(total_due, number_of_people) print("Each person owes ${}".format(amount_due))

What does float do here in the total_due variable?

2 Answers

Jose Borges
seal-mask
.a{fill-rule:evenodd;}techdegree
Jose Borges
Python Web Development Techdegree Student 2,953 Points

It's converting the input of "What is the total?" which would be a string type into a floating number type, a decimal, which is how money would be represented. Similarly the next variable (number_of_people) converts an input (string) into an integer (since people wouldn't be represented as decimals). This allows them to be used in the arithmetic function split_check().