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 Expecting Exceptions

Michael Motes
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Motes
Front End Web Development Techdegree Student 1,651 Points

NameError: name 'total' is not defined

Hi Everyone. I have been having been struggling when it comes to finding the solution to solve the NameError: name 'total' is not defined.

This happens every time I run the check_please.py file, and unsure why that is. Would love to know the solution to this. Thank you.

Michael Motes
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Motes
Front End Web Development Techdegree Student 1,651 Points

Project based on splitting a check with people you go with to a restaurant

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 valued value. Try again...")
else:
    amount_due = split_check(total, number_of_people)
    total = "anything you want"
    print("Each person owes ${}".format(amount_due))
treehouse:~/workspace$ python check_please.py                                                                                                        
What is the total?  20                                                                                                                               
How many people?  0                                                                                                                                  
Traceback (most recent call last):                                                                                                                   
  File "/home/treehouse/workspace/check_please.py", line 14, in <module>                                                                             
    amount_due = split_check(total, number_of_people)                                                                                                
NameError: name 'total' is not defined 

This is my result so far. Just wanted to know how where total = "anything you want" should be placed.

1 Answer

olushola oludipe
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
olushola oludipe
Web Development Techdegree Graduate 18,495 Points

okay Michael, firstly when is said (total = "anything you want") is was trying to show you the syntax for declaring a varible, so I'm sorry if I confused you there. now back to your code. first remove (total = "anything you want"): It's not useful, that was just an example. now the problem with your code is ( amount_due = split_check(total, number_of_people) ) . amount_due = split_check(total_due, number_of_people) is the correct thing to do.

your parameter "number_of_people" is defined on line 8 which is correct, now the problem is the parameter "total" you didn't define that, but you have "total_due" so your first parameter should be "total_due"

I hope this helps.