Python Forum

Full Version: coding error iro a menu
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The code below when run ignores my menu and goes straight into the first def xx() command - WHY

import sqlite3
def mainMenu():

print(' \033[1mMenu program ')
print(' Press the number next to your choice ')
print(" 1 - Add Numbers to DataBase ")
print(" 2 - Print all the Data ")
print(" 3 - Add Numbers to DataBase ")
print(" 4 - Add Numbers to DataBase ")
print(" 5 - Exit the program ")
while True:
try:
selection=int(input('Enter choice: '))
if selection ==1:
addnewnames()
break
elif selection ==2:
list()
break
if selection ==3:
update()
break
elif selection ==4:
delete()
break
elif selection ==5:
print(' You selected 5 - the QUIT key \n Bye Bye!')
break

else:
print()
print(" INVALID SELECTION. \n Please enter a number between 1 and 3 ")
except ValueError:
print('Invalid choice \n Enyer anumber between 1 and 3')
exit

def addnewnames():
import sqlite3
conn = sqlite3 . connect ( 'phonebook.db' )
cursor = conn.cursor ()

s_SURNAME = input('Surname :')
s_FIRSTNAME = input('Firstname :')
s_NUMBER = input('Number :')
s_EMAILADDRESS = input ('EmailAdress :')
print()
print ( 'Posting information to database')
print()
cursor.execute("""
INSERT INTO phonedata(surname, firstname, number, emailaddress)
VALUES (?,?,?,?)
""", (s_SURNAME, s_FIRSTNAME, s_NUMBER, s_EMAILADDRESS))
conn.commit ()
print ( 'Data entered successfully.' )
conn . close ()
if (conn):
conn.close()
print("\nThe SQLite connection is closed.")
mainMenu()

def list():
import sqlite3
conn = sqlite3 . connect ( 'phonebook.db' )
cursor = conn.cursor ()
print ("Opened database successfully");
cursor = conn.execute("SELECT ID, SURNAME, FIRSTNAME, NUMBER, EMAILADDRESS from phonedata")
for row in cursor:
print ("ID = ", row[0])
print ("SURNAME = ", row[1])
print ("FIRSTNAME = ", row[2])
print ("NUMBER = ", row[3])
print ("EMAILADDRESS = ", row[4], "\n")

print ("Operation done successfully");
conn.close()
mainMenu()

def update():
print ('UPDATE')
anykey=input('Hit any key to continue')
mainMenu()

def delete():
print ('DELETE')
anykey=input('Hit any key to continue')
mainMenu()

mainMenu()
Sorry about the first posting with no indentations - I 'Cut & Pasted' from honny program and when pasted it the indents were there but when I press send they disappear. I will have to find out why!
You need to surround code with Python tags. I think I got the indentation correct and the code works for me.
def mainMenu():

    print(' \033[1mMenu program ')
    print(' Press the number next to your choice ')
    print(" 1 - Add Numbers to DataBase ")
    print(" 2 - Print all the Data ")
    print(" 3 - Add Numbers to DataBase ")
    print(" 4 - Add Numbers to DataBase ")
    print(" 5 - Exit the program ")
    while True:
        try:
            selection=int(input('Enter choice: '))
            if selection ==1:
                addnewnames()
                break
            elif selection ==2:
                list()
                break
            elif selection ==3:
                update()
                break
            elif selection ==4:
                delete()
                break
            elif selection ==5:
                print(' You selected 5 - the QUIT key \n Bye Bye!')
                break
            else:
                 print(" INVALID SELECTION. \n Please enter a number between 1 and 3 ")
        except ValueError:
            print('Invalid choice \n Enyer anumber between 1 and 3')
            exit

def addnewnames():
    print('addnewnames')
    input('Hit any key to continue')
    mainMenu()

def list():
    print('list')
    input('Hit any key to continue')
    mainMenu()

def update():
    print ('update')
    input('Hit any key to continue')
    mainMenu()

def delete():
    print ('delete')
    input('Hit any key to continue')
    mainMenu()

mainMenu()
Using list as a variable or function name hides the builtin list() function.