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

Still confused about try method

I still can't understand why the variable defined in try method will return name error in the lecture video?

Can you share your code in question, or make a snapshot of your workspace to share so that we can more easily assist you?

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("number of people?')) except ValueError: print("Oh no! That's not a valid value. Try again...") amount_due = split_check(total_due, number_of_people) print("Each person owes ${}".format(amount_due))

I'm not able to find the option to add a snapshot but when I pass any string in this function, it returns name error for total_due and this is explained in the video but I couldn't understand.

For clarification, when you use the Markdown Cheatsheet to format your code, it will look like this:

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("number of people?')) except ValueError: print("Oh no! That's not a valid value. Try again...") amount_due = split_check(total_due, number_of_people) print("Each person owes ${}".format(amount_due))

3 Answers

Oh nevermind, I saw the video again carefully and understood what was wrong. Thanks for helping.

I'm glad I could help get you closer. :)

When looking at your code formatted,

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("number of people?')) except ValueError: print("Oh no! That's not a valid value. Try again...") amount_due = split_check(total_due, number_of_people) print("Each person owes ${}".format(amount_due))

You can see you used the wrong quote to close out this section

int(input("number of people?'))

You should use the double quote:

int(input("number of people?"))

Now your code is formatted correctly:

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("number of people?")) except ValueError: print("Oh no! That's not a valid value. Try again...") amount_due = split_check(total_due, number_of_people) print("Each person owes ${}".format(amount_due))

Still gives the name error when passing a string

Can you share a link to the video in question? Your question here isn't linking to it.

Thanks.

https://teamtreehouse.com/library/expecting-exceptions At 4:12 when he runs the code for this first time, it gives an error