Question

Use python to code!

Q1 - You need to keep track of book titles. Write code using function(s)
that will allow the user to do the following.


1. Add book titles to the list.
2. Delete book titles anywhere in the list by value.
3. Delete a book by position.
Note: if you wish to display
the booklist as a vertical number booklist , that is ok
(that is also a hint)
4. Insert book titles anywhere in the list.
5. Clear the list (remove all items from the list)
6. Print the list horizontally using sys.stdout.write()
7. Sort the list
8. The user can add, remove, insert, clear, delete, print books as
much as they wish.

The person will add or remove books, one book at at a time
When a book is add, deleted, inserted or sorted the list should
automatically be printed

Use the template below #this is a global variable. booklist [] It can be used by all the functions def printBooklist(): # pri

Thank you!!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Code

import sys

#this is a global variable. It can be used by all the functions

bookList = ['The Novel of Kings','The Fight of Identity']

def printBookList():

  # print the items of the list using a for statement

  print()

  print("------------- Book List --------------")

  for i in bookList:

    print("{}".format(i))

  print()

def addBook():

  # add a book to the list

  book_name = input("Enter the Book Name : ")

  #Using append that is in inbuilt function of list

  #It take's an element and add it to the end of list

  bookList.append(book_name)

def deleteBookByTitle():

  # remove a book from the list

  book_title = input("Enter the Book Title you want to delete : ")

  #Using remove that is in inbuilt function of list

  #It take's an element and remove it from the list

  bookList.remove(book_title)

def deleteBookByPosition():

  # remove a book by asking the user what position.

  # HINT - you may want to display the booklist a a vertical numbered list

  for i in range(len(bookList)):

    print("{} : {}".format(i+1,bookList[i]))

  book_position = int (input("Enter the position of the Book you want to delete : "))

  #Using pop that is in inbuilt function of list

  #It take's an element's position and remove it from the list

  bookList.pop(book_position - 1)

  #del bookList[book_position - 1]


def insertBookByPosition():

  # insert a book by inserting it a particular spot

  # NOTE: programs know to start at zeor - users do not

  # Adjust thier input appropriately

  # HINT - you may want to display the booklist a a vertical numbered list

  for i in range(len(bookList)):

    print("{} : {}".format(i+1,bookList[i]))

  User_book_insert = input("Enter the book Name you want to insert : ")

  User_position = int (input("Enter the position you want to do the book in : "))

  #Using insert that is in inbuilt function of list

  #It take's an position and element and then insert it to the list

  bookList.insert(User_position-1 , User_book_insert)

def sortBookLIst():

  #sort the list

  bookList.sort()

def clearBookList():

  # remove all of the books

  bookList.clear()

def printHorizontally():

  for i in bookList:

    sys.stdout.write(i+' | ')

  print()

def menu():

  # menu system

  print('-----------------------------------------------------')

  print('1. Add book titles to the list.')

  print('2. Delete book titles anywhere in the list by value.')

  print('3. Delete a book by position.')

  print('4. Insert book titles anywhere in the list.')

  print('5. Clear the list')

  print('6. Print the list horizontally ')

  print('7. Sort the list')

  print('8. Quit')

  print()

def main():

  #Will run the main till user quits.

  while(True):

    menu()

    #Taking User choice and calling the functions accordingly.

    user_choice = int (input("Enter your Choice : "))

    if user_choice==1:

      addBook()

      printBookList()

    elif user_choice==2:

      printBookList()

      deleteBookByTitle()

      printBookList()

    elif user_choice==3:

      deleteBookByPosition()

      printBookList()

    elif user_choice==4:

      insertBookByPosition()

      printBookList()

    elif user_choice==5:

      clearBookList()

    elif user_choice==6:

      printHorizontally()

    elif user_choice==7:

      sortBookLIst()

      printBookList()

    else:

      print()

      print("-------   Thanks ^_^  -----------------")

      break

main()

#Output

#this is a global variable. It can be used by all the functions booklist = [The Novel of Kings, The Fight of Identity] de

def insertBookByPosition(): # insert a book by inserting it a particular spot # NOTE: programs know to start at zeor - users

user_choice int (input(Enter your choice : )) if user_choice--1: addBook) printBooklist() elif user_choice--2: printBooklis

1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. 4. Insert

1 1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. 4. Inser

1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. 4. Insert

#Note If need any help then ping me in the comments I will be glad to help you out ^_^ and If you liked the answer and explanation then please give a thumbs up I really need a +ve feedback at the moment.

Add a comment
Know the answer?
Add Answer to:
Use python to code! Q1 - You need to keep track of book titles. Write code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this python program to access an excel file, books inventory file. The file called...

    I need this python program to access an excel file, books inventory file. The file called (bkstr_inv.xlsx), and I need to add another option [search books], option to allow a customer to search the books inventory, please. CODE ''' Shows the menu with options and gets the user selection. ''' def GetOptionFromUser(): print("******************BookStore**************************") print("1. Add Books") print("2. View Books") print("3. Add To Cart") print("4. View Receipt") print("5. Buy Books") print("6. Clear Cart") print("7. Exit") print("*****************************************************") option = int(input("Select your option:...

  • Python: I need help with the following quit function ! The quit menu option should not...

    Python: I need help with the following quit function ! The quit menu option should not be case sensitive - 'q' or 'Q' should quit the program menu.add_option('Q', 'Quit', quit_program) NB: Remaining code ! """ Program to create and manage a list of books that the user wishes to read, and books that the user has read. """ from bookstore import Book, BookStore from menu import Menu import ui store = BookStore() def main(): menu = create_menu() while True: choice...

  • python programming: Can you please add comments to describe in detail what the following code does:...

    python programming: Can you please add comments to describe in detail what the following code does: import os,sys,time sl = [] try:    f = open("shopping2.txt","r")    for line in f:        sl.append(line.strip())    f.close() except:    pass def mainScreen():    os.system('cls') # for linux 'clear'    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print(" SHOPPING LIST ")    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print("\n\nYour list contains",len(sl),"items.\n")    print("Please choose from the following options:\n")    print("(a)dd to the list")    print("(d)elete from the list")    print("(v)iew the...

  • In this project, you will use functions and dictionaries to track basketball players and their respective...

    In this project, you will use functions and dictionaries to track basketball players and their respective points, then display statistics of points made. You will need three functions as follows: def freeThrowMade(playerDictionary, playerName) - this function will add 1 point to the player's total score def twoPointMade(playerDictionary, playerName) - this function will add 2 points to the player's total score def threePointMade(playerDictionary, playerName) - this function will add 3 points to the player's total score Each of these functions has...

  • Python please For this program, we will be making lists for the local Zoo. You have...

    Python please For this program, we will be making lists for the local Zoo. You have been provided with 4 lists of data already created for you. Using this data, we update the data at this Zoo. YOU MUST USE LIST FUNCTIONS FOR REMOVING TO RECEIVE YOUR FULL POINTS! Input: A type of animal -- This information should be provided in response the question: "What animal are we removing?" A number corresponding to the selection of a menu number (see...

  • In Python !!! In this exercise you will continue to work with Classes and will build...

    In Python !!! In this exercise you will continue to work with Classes and will build on the Book class in Lab 13.10; you should copy the code you created there as you will need to extend it in this exercise. You will extend the Book class to accommodate the case where there may be multiple authors for a book. The attribute author should become a list. The constructor will still take a parameter that is a string for author,...

  • C++ : Please include complete source code in answer This program will have names and addresses...

    C++ : Please include complete source code in answer This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record. When the program is run, it will search for a birthday or an anniversary using the current date to compare with the saved date. It will then generate the appropriate card message. Because this will be an interactive system, your program should begin by displaying a...

  • Java code: Use at least one array defined in your code and one array or array...

    Java code: Use at least one array defined in your code and one array or array list defined by user input.    Examples of arrays you can define in code are: A deck of cards, the days of the week, the months of a year. User input can be things like grades, shopping list items, wish lists, deposits an withdrawals, etc. Be able to add, remove, print, sort as necessary to complete the program's goal. Thanks

  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT