Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
project error code
#1
when i run my program i get an error code ValueError: invalid literal for int() with base 10: ''
in line 114, in <module>
response = input("Add"+book_list[int(item_pic)-1][0]+" y/n? ")
import os # include this import only if not done elsewhere in your code

more_carts = "y"
carts =0
cart_items = 0
cart_cost = 0.0
session_carts = 0
session_items = 0
session_cost = 0.0

def itemMenu (category, itemList):
    """itemMenu function - displays menu of variable number of shopping items.
       Inputs: category (books, etc.), list of item descriptions and prices.
       Returns: selected menu item (integer, 1 to n), d, or x
       ---------------------------------------------------------------------"""  
    os.system('cls')
    print ('\n\n\t\t ' + category + ' menu')
    print ('\n\t\t Select from the following items, display cart, or checkout: ')
    print('\n\t\t\t {0:3s}  {1:26s}  {2:8s}'.format('No.', 'Item Description', 'Price '))
    print('\t\t\t {0:3s}  {1:26s} {2:8s}'
          .format('===', '===========================', '========'))
    for n in range(0, len(itemList)):
        print('\t\t\t {0:>2s} - {1:26s}  ${2:8.2f}'.format(str(n+1), itemList[n][0], itemList [n][1]))
    print('\n\t\t\t {0:>2s} - {1:26s} '.format('d',  'display cart contents '))
    print('\t\t\t {0:>2s} - {1:26s} '.format('x', 'return to category menu '))
    menuPic = input('\n\nEnter Selection (1 to {0:>2s}, "d", or "x"): '.format(str(len(itemList))))
    return menuPic 

# Only cut/past the above code.  The code below does a basic demo of using
# the above function.  There is no reason to include it in your code.

category_menu = """\n\n
             Select an item from the following menu or checkout:
             
               1 = Books 
               2 = electronics
               3 = clothing
               d = show cart contents
               c = checkout
        
        """

# category items tuples - one per category --------------------------------
#------------books
infile = "books.txt"
fi = open(infile,"r")
book_item = {}
for line in fi:
    list1 = line.split(",")
    des = list1[0]
    price = list1[1]
    book_item[des] = float(price.strip())
fi.close()
book_list = []
for i in book_item:
    k = (i,book_item[i])
    book_list.append(k)
 #----------electronic   
infile1 = "electronics.txt"
fi = open(infile1,"r")
electronic_item = {}
for line in fi:
    list1 = line.split(",")
    des = list1[0]
    price = list1[1]
    electronic_item[des] = float(price.strip())
fi.close()
elect_list = []
for i in electronic_item:
    k = (i,electronic_item[i])
    elect_list.append(k)
#-----------clothes    
infile2 = "clothing.txt"
fi = open(infile1,"r")
clothing_item = {}
for line in fi:
    list1 = line.split(",")
    des = list1[0]
    price = list1[1]
    clothing_item[des] = float(price.strip())
fi.close()
cloth_list = []
for i in clothing_item:
    k = (i,electronic_item[i])
    cloth_list.append(k)

# start of active code*********************************
while more_carts == "y":
    books_items = 0
    books_cost = 0.0
    elect_items = 0
    elect_cost = 0.0
    clothing_items = 0
    clothing_cost = 0.0
    cart = []

    more_items = "y"
    while more_items == "y":
        os.system('cls')
        print(category_menu)
        category = input('Select category (1 - 3), d, or x :')
        if category == "c":
            break
        elif category == "d":
            print("shopping cart ",cart)
        elif category not in "123":
            print("Invalid category selected")
            continue
        if category == '1':
            item_pic = "1"
            while item_pic != "x":
                os.system('cls')
                item_pic = input(itemMenu('Books', book_list))
                response = input("Add"+book_list[int(item_pic)-1][0]+" y/n? ")
                if response == "y":
                    cart.append(book_list[int(item_pic)-1])
                    print("shopping cart = ", cart)
                else:
                    continue
                if item_pic == "d":
                    print("shopping cart = ",cart)

                    
        elif category == '2':
            item_pic = "1"
            while item_pic != "x":
                os.system('cls')
                item_pic = input(itemMenu('Electronics', elect_list))
                response = input("Add "+elect_list[int(item_pic)-1][0]+" y/n? ")
                if response == "y":
                    cart.append(elect_list[int(item_pic)-1])
                    print("shopping cart = ", cart)
                else:
                    continue
                if item_pic == "d":
                    print("shopping cart = ",cart)
              
                    
        elif category == "3":
               item_pic = "1"
               while item_pic != "x":
                   os.system('cls')
                   item_pic = input(itemMenu ('clothing', cloth_list))
                   response = input("Add "+clothing_list[int(item_pic) -1][0]+" y/n? ")
                   if response == "y":
                       cart.append(clothing_list[int(item_pic)-1])
                       print("shopping cart = ", cart)
                   else:
                       continue
                   if item_pic == "d":
                        print("shopping cart = ",cart)
#------------------------------------------------
    if category == "c":
        print("\nCheckout Selected.....")
        
    if len(cart) > 0:
        cart_total_cost = 0.0
        print("\n\t{0:20s}\t\t{1:4s}".format("items", "cost"))
        print("\t{0:20s}\t\t{1:9s}".format("====================","========="))
        for items in cart:
            print("\t{0:20s}\t\t${1:8.2f}".format(items[0],items[1]))
            cart_total_cost += items[1]
        print("\nTotal number of items: {0:5d}    cost: ${1:8.2f}".format(len(cart), cart_total_cost))
        session_carts += 1
        session_items += len(cart)
        session_cost +=cart_total_cost
    else:
        print("\nCart is empty\n")
        
more_carts = input("\nAre there more carts to process (y/n)?\n")
print("\n\n\tTotal number of carts: ", session_carts)
print("\tTotal number of items: ",session_items)
print("\tTotal cost of items: $%8.2f" %(session_cost))
input ("\n\nHit Enter to end program")
Reply
#2
The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
Note that the full traceback also shows you the line number on which the exception occurred, so you can work backwards from there.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked School project -- need help with it it says syntax error XPGKNIGHT 6 3,256 Aug-11-2022, 08:43 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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