#๐Ÿ”’ Need a bit of clarification.

20 messages ยท Page 1 of 1 (latest)

rich agate
#

I have this code below:

from classes.database_access import DB_Connect

my_db = DB_Connect('root', '', 'family_bookstore')

print("Welcome to our family bookstore! Select an option from below:")

from functions.show import show_all_books
from functions.add import add_book
from functions.record import record_book_sale
from functions.edit import edit_book_details
from functions.remove import remove_book

def main():
    while True:
        print("1. Show all books")
        print("2. Add a book")
        print("3. Record a book sale")
        print("4. Edit book details")
        print("5. Remove a book")
        print("6. Exit program")

        option = input("\nSelect an option: ")

        if option == '1':
            show_all_books()
        elif option == '2':
            add_book()
        elif option == '3':
            record_book_sale()
        elif option == '4':
            edit_book_details()
        elif option == '5':
            remove_book()
        elif option == '6':
            print("Exiting program.")
            break
        else:
            print("Invalid option. Please try again.")

if __name__ == "__main__":
    main()

I sent it to my professor and this is what he said after I told him about the error that I got in the picture.
"You do not need that main function in your main program. Getting rid of it will let your code access the database variable you created on line 3. Then pass that variable to your functions and you should be OK!"

Is he saying to take out the entire code under the def main() and save it to a separate file to have it imported like I did the ones above and put the my_db function in it or??

junior zodiacBOT
#

@rich agate

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

true chasm
#

imports cant access variables in the importer file

#

youd need to pass my_db to the function

rich agate
#

So then do I just put it under the main function?

#

@true chasm

true chasm
#

you pass it as a parameter

rich agate
true chasm
#

to the function that uses my_db

#

not main

#

functions.show

rich agate
lone sparrow
#

you have to pass the argument

#

you defined the function with a parameter, so you have to pass an argument when calling the function

#

i guess you want to pass the function your my_db variable, so you have to call show_all_books(my_db) instead of show_all_books()

rich agate
#

Ohh okay, I got it to work now, thanks for the help!

junior zodiacBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.