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 trialMichael Motes
Front End Web Development Techdegree Student 1,651 PointsNameError: 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
Front End Web Development Techdegree Student 1,651 PointsProject 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
Web Development Techdegree Graduate 18,495 Pointsokay 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.
Michael Motes
Front End Web Development Techdegree Student 1,651 PointsYep, this helped. Thank you so much for taking the time to explain.
olushola oludipe
Web Development Techdegree Graduate 18,495 Pointsolushola oludipe
Web Development Techdegree Graduate 18,495 PointsI think you didn't define the variable "total"...it should look something like this total = "anything you want" hope this helps.