Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coding error iro a menu
#1
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()
Reply
#2
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!
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding error. xflyerwdavis 2 523 Oct-07-2023, 07:08 PM
Last Post: deanhystad
  Coding error. Can't open directory EddieG 6 1,109 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Coding Error EddieG 2 532 Jul-09-2023, 02:59 AM
Last Post: EddieG
  python coding error isntitzee 1 2,202 Oct-17-2020, 06:30 AM
Last Post: buran
  Coding error- Not sure where I have put error markers against the code that is wrong Username9 1 1,728 Sep-28-2020, 07:57 AM
Last Post: buran
  Adafruit LIS3DH coding error FitDad73 1 2,000 Aug-30-2020, 01:37 AM
Last Post: bowlofred
  Coding error- help FatherYeals 3 32,181 Mar-27-2020, 02:11 PM
Last Post: deanhystad
  name undefined Coding Error Ewilliam51 2 2,137 Feb-06-2020, 12:19 AM
Last Post: Ewilliam51
  coding error bilawal 11 5,445 Dec-07-2019, 05:23 PM
Last Post: DreamingInsanity
  Pyhton Coding Help Needed (very small error) friduk 2 2,443 Oct-23-2019, 08:41 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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