Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError. Please Help
#1
import sqlite3
con=sqlite3.connect("contactbook.db")
cursor=con.cursor()

def add_contact(group,first_name,last_name,title,mobile):
	cursor.execute("CREATE TABLE IF NOT EXISTS contacts (group TEXT, first_name TEXT, last_name TEXT, title TEXT , mobile INT)")
	con.commit()
	cursor.execute("INSERT INTO contacts VALUES(?,?,?,?,?)",(group,first_name,last_name,title,mobile))
	con.commit()
	group=input("Group:")
	first_name=input("First Name:")
	last_name=input("Last Name:")
	title=input("Title:")
	mobile=int(input("Mobile:"))
	con.close()
def menu():
	try:
		ch1=int(input("[1] - All Contacts\n[2] - Add Contact\n[3] - Edit Contact\n\nChoose One:"))
		if ch1==1:
			all_contacts()
		if ch1==2:
			add_contact(group, first_name, last_name, title, mobile)
		if ch1==3:
			edit_contact()
	except ValueError:
		print("Wrong Format!")


menu()
Error:
[1] - All Contacts [2] - Add Contact [3] - Edit Contact Choose One:2 Traceback (most recent call last): File "asdasdasdasdasd.py", line 29, in <module> menu() File "asdasdasdasdasd.py", line 22, in menu add_contact(group, first_name, last_name, title, mobile) NameError: name 'group' is not defined
Reply
#2
There are multiple issuies:
1.when you pass group as argument when you call add_contact on line 22 none of group, first_name, last_name, title, mobile is defined. One way to deal with this is collect this information before you call the function, then pass it as argument. Another way is to remove the parameters of add_contact() and collect the information inside the function. However here comes next issue:
2 You collect that information in the body of add_contact(), but at the end, not before you try to use this information
3. all_contacts() and edit_contact() are not defined.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,379 Oct-27-2019, 07:49 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020