i'm writing a function as part of a larger overall class project, and i'm having multiple issues with this looping statement, could any of you tell me how it's not looping even though its not meeting the exit criteria? i get what i need up until the loop, i'm trying to get it to ask you to select an item to add, print out the print statement, then start over and if you select the same item twice, add it to the cart
print("**********ITEMS**********")
print("1................Shirts - $10")
print("2.................Pants - $20")
print("3.................Shoes - $40")
print("4.................Dress - $50")
print("Complete Purchase")
cart = []
items = 0
def addItems():
items = int(input("please add an item to your cart: "))
while items > 0:
if items == 1:
print("Shirts,{}x,${}".format(+1, +10))
cart.count("Shirt")
cart.append("Shirt")
print(cart)
return cart
elif items == 2:
print("Pants,{}x,${}".format(+1, +20))
#cart.count("Pants")
cart.append("Pants")
return cart
elif items == 3:
print("Shoes,{}x,${}".format(+1, +40))
#cart.append("Shoes")
cart.count("Shoes")
return cart
elif items == 4:
print("Dress,{}x,${}".format(+1, +50))
#cart.append("Dress")
cart.count("Dress")
return cart
elif items > 5:
break
#return cart
# print(cart.count("Shirt") * 10 + cart.count("Pants") * 20 + cart.count("Shoes") * 40 + cart.count("Dress") * 50)
addItems()
this is my output below
please add an item to your cart: 1
Shirts,1x,$10
['Shirt']
Process finished with exit code 0
You should remove 'return cart' from your if-elif ladder and add it to outside of the while loop. Also you could take input from the user in while loop in addition to the previous one, at the end of the while loop.
def addItems():
items = int(input("please add an item to your cart: "))
while items > 0:
if items == 1:
print("Shirts,{}x,${}".format(+1, +10))
cart.count("Shirt")
cart.append("Shirt")
print(cart)
elif items == 2:
print("Pants,{}x,${}".format(+1, +20))
#cart.count("Pants")
cart.append("Pants")
elif items == 3:
print("Shoes,{}x,${}".format(+1, +40))
#cart.append("Shoes")
cart.count("Shoes")
elif items == 4:
print("Dress,{}x,${}".format(+1, +50))
#cart.append("Dress")
cart.count("Dress")
elif items > 5:
break
items = int(input("please add an item to your cart: "))
return cart
Related
My remove and new quantity methods dont work and I have no idea why. Here is my code:
# Type code for classes here
class ItemToPurchase():
def __init__(self):
self.item_description = 'none'
self.item_name = 'none'
self.item_price= 0
self.item_quantity = 0
def print_item_description(self):
print(f'{self.item_name}: {self.item_description}')
class ShoppingCart():
def __init__(self, customer_name='none', current_date='January 1, 2016'):
self.customer_name = customer_name
self.current_date = current_date
self.items = []
def add_item(self, item ):
''' Adds an item to cart_items list. Has parameter of type ItemToPurchase. Does not return anything. '''
self.items.append(item)
def remove_item(self, item ):
''' Removes item from cart_items list. Has a string (an item's name) parameter. Does not return anything.
If item name cannot be found, output this message: Item not found in cart. Nothing removed. '''
delete = False
for item in self.items:
if self.items == item:
to_be_deleted = self.items[item]
delete = True
else:
print("Item not found in cart. Nothing removed.")
if delete == True:
del self.items[to_be_deleted]
def modify_item(self, item, new_quantity):
'''Modifies an item's quantity. Has a parameter of type ItemToPurchase. Does not return anything.
If item can be found (by name) in cart, modify item in cart.'''
if item in self.items:
item.quantity = new_quantity
else:
print("Item not found in cart. Nothing modified.")
def get_num_items_in_cart(self):
'''Returns quantity of all items in cart. Has no parameters.'''
num_items = 0
for item in self.items:
num_items += item.item_quantity
#return the num_Items
return num_items
def get_cost_of_cart(self):
'''Determines and returns the total cost of items in cart. Has no parameters.'''
return sum(item.item_price * item.item_quantity for item in self.items)
def print_total(self):
'''Outputs total of objets in cart.
If cart is empty, outputs this message: CART IS EMPTY.'''
print(f"{self.customer_name}'s Shopping Cart - {self.current_date}")
number_items = self.get_num_items_in_cart()
print(f'Number of Items: {number_items}\n')
total_cost = self.get_cost_of_cart()
if total_cost == 0:
print("SHOPPING CART IS EMPTY\n")
print(f'Total: ${total_cost}')
return False
else:
for item in self.items:
item_cost = item.item_quantity * item.item_price
print(f'{item.item_name} {item.item_quantity} # ${item.item_price} = ${item_cost}')
print()
total_cost = self.get_cost_of_cart()
print(f'Total: ${total_cost}')
def print_descriptions(self):
''' Outputs each item's description'''
print(f"{self.customer_name}'s Shopping Cart - {self.current_date}\n")
for item in self.items:
print("Item Descriptions")
print(f'{item.item.name}: {item.item_description}')
def print_menu():
print("MENU\na - Add item to cart\nr - Remove item from cart\nc - Change item quantity\ni - Output items' descriptions\no - Output shopping cart\nq - Quit\n")
def execute_menu(choice , shopping_cart):
if choice == "o":
print("OUTPUT SHOPPING CART")
shopping_cart.print_total()
elif choice == "i":
print("OUTPUT ITEMS' CART")
shopping_cart.print_descriptions()
elif choice == "a":
print("ADD ITEM TO CART\nEnter the item name:")
item_name = input()
print("Enter the item description:")
item_description = input()
print("Enter the item price:")
item_price = int(input())
print("Enter the item quantity:")
item_quantity = int(input())
New_item = ItemToPurchase()
New_item.item_name = item_name
New_item.item_price = item_price
New_item.item_quantity = item_quantity
New_item.item_description = item_description
shopping_cart.add_item(New_item)
elif choice =="r":
print("REMOVE ITEM FROM CART\nEnter name of item to remove:")
removed_item = input()
shopping_cart.remove_item(removed_item)
elif choice == "i":
'''Implement Change item quantity menu option in execute_menu(). Hint: Make new ItemToPurchase object before using ModifyItem() method. '''
print("CHANGE ITEM QUANTITY\nEnter the item name:")
Item = ItemToPurchase()
Item.item_name = input()
print("Enter the new quantity:")
Item.item_quantity = input()
if __name__ == "__main__":
shopping_cart = ShoppingCart()
print("Enter customer's name:")
shopping_cart.customer_name = input()
print("Enter today's date:")
shopping_cart.current_date = input()
print()
print(f"Customer name: {shopping_cart.customer_name}\nToday's date: {shopping_cart.current_date}")
print()
print_menu()
print("Choose an option:")
while True:
choice = input()
if choice in "arcioq":
if choice == 'q':
break
else:
execute_menu(choice, shopping_cart)
print()
print_menu()
print("Choose an option:")
I think with remove I'm comparing wrong data types and i cant seem to make it work. By the way, the function names cannot change and i cannot add any new functions. I can only use doc strings of preexisting methods and classes and functions
You're not using del correctly. I think you've confused del with the .remove() method of a list.
del self.items[to_be_deleted] makes no sense. self.items is a list, and list indexes must be integers. But to_be_deleted is not an integer; it is an actual item in the list.
If you want to use del, you need the integer index of the list item to be removed.
But since you have the actual object itself, you can call self.items.remove() instead, which takes the actual object as an argument.
Hi I'm not sure if I've used the right terminology, but I've been assigned a homework for OOP in Python. We have to make a basket system that users can add items to etc... I've completed the homework but I can't figure out why something that I did worked.
I have two classes, Basket() and Shop(). But in the checkout function in Basket, I've used Shop.getPrice(Shop, x). For the life of me I can't figure out why this works.
Also please ignore the sheer number of comments, I had to explain my code to someone.
Thank you in advance
class Shop():
def __init__(self):
self.items = {"eggs":1.95, "bread":0.5, "cheese":2.45}
def addItem(self, item, price):
item = item.lower() #? lower case
self.items[item] = price #? add item to dictionary with price
def deleteItem(self, item):
if item in self.items:
self.items.pop(item) #? remove item from dictionary
else:
print("Item not found") #? item not found
def getPrice(self, item):
return self.items[item] #? returns the price of item
#! DONT LOOK AT THIS FUNCTION
def changePrice(self, item, newPrice):
if newPrice > 0:
self.items.update({item:newPrice})
else:
print("Price must be greater than 0")
def getInput(self):
choice = int(input("""
1. Display Basket
2. Select Item
3. Checkout
4. Exit
Please enter your choice: """))
return choice #? return the input
def main(self):
running = True
basket = Basket() #? instantiate basket
while running:
choice = shop.getInput() #? get input from user
if choice == 1:
basket.showBasket() #? show basket
elif choice == 2:
#! Select Item
found = False #? sets up loop
while not(found): #? loop until item is found
choice = input("Enter the name of the item: ")
choice.lower()
if choice in shop.items: #? checks if item is in shop
basket.addToBasket(choice) #? add item to basket
found = True #? exit loop
else:
print("Item not found, please try again")
elif choice == 3:
#! Checkout
basket.checkout() #? run checkout function
elif choice == 4:
#! Exit
running = False #? exit loop
else:
print("Invalid choice")
shop = Shop()
class Basket():
def __init__(self):
self.contents = {} #? basket is empty to begin with
def addToBasket(self, item):
if item in self.contents: #? if item is in basket, add 1 to quantity
self.contents[item] += 1
else: #? if item is not in basket, add item to basket
self.contents[item] = 1
def removeFromBasket(self, item):
if item in self.contents:
if self.contents[item] > 1: #? if item is more than 1, remove 1
self.contents[item] -= 1
else: #? else remove item
self.contents.pop(item)
else:
print("Item not found")
def checkout(self):
total = 0
for x in self.contents:
total += self.contents[x] * Shop.getPrice(Shop, x)
print("Total: " + str(total))
def showBasket(self):
for x in self.contents:
print(self.contents[x])
shop.main()
Your function Shop.getPrice(Shop, x), worked because you have the classes in the same file, and Shop() class it's in the scope of Basket() class
I'm trying to use menus to accepts menu options that append an empty list that acts as a cart. When the list is done with i have the option to add more lists if necessary. In the end im supposed to calculate the total number of carts, total number of items, and the total price. The first issue is the calcuation of carts is wrong as it treats every new entry as list rather than na item, which elads to the item count being wrong as well for each cart. Along with this, i get "TypeError: unsupported operand type(s) for +: 'int' and 'str'" when trying calculating the final price and im just not sure what to do
def main():
#Flag for full checking out or not
checkout = False
#Flag to start a new cart or not
new_cart = True
#Placeholder list
cart = []
#List of items
book_list = [['My Own Words', 18.00], ['Grant', 24.50], ['The Overstory', 18.95], ['Becoming', 18.99]]
elec_list = [['HP Laptop', 429.50], ['Eyephone', 790.00], ['Bose Speakers', 220.00]]
cloth_list = [['T-shirt', 9.50], ['Shoes', 45.00], ['Pants', 24.00], ['Nationals Hat', 32.00]]
groc_list = [['Coho Salmon', 12.50], ['Spaghetti', 2.75], ['Milk', 3.99], ['Eggs', 1.99], ['Flat Tire Ale', 9.95]]
while checkout == False or new_cart == True:
#Main Menu
if checkout == False:
#Main Item menu
print("""
1 - Books
2 - Electronics
3 - Clothes
4 - Groceries
c - Continue to checkout
""")
choice = input("Select one of the categories or checkout(1-4 or 'c'): ")
#Variable to return user to past menu
Return = False
if choice == '1':
while Return == False:
#Sub item menu
print("""
1 - "My Own Words", $18.00
2 - "Grant", $24.50
3 - "The Overstory", $18.95
4 - "Becoming", $18.99
x - return to menu
""")
item = input("Please select from the menu or go back to the categories: ")
if item == '1' or item == '2' or item =='3' or item == '4':
#Adds item onto the the cart
cart.append(book_list[int(item)-1])
elif item == 'x':
#Returns user to main menu
Return = True
else: print("Invalid input try again")
elif choice == '2':
while Return == False:
#Sub item menu
print("""
1 - HP Laptop, $429.50
2 - EyePhone 10, $790.00
3 - Bose 20 Speakers, $220.00
x - return to menu
""")
item = input("Please select from the menu or go back to the categories: ")
if item == '1' or item == '2' or item == '3':
#Adds item onto the the cart
cart.append(elec_list[int(item)-1])
elif item == 'x':
Return = True
else:
print("Invalid input try again")
elif choice == '3':
while Return == False:
#Sub item menu
print("""
1 - T-shirt, $9.50
2 - Shoes, $45.00
3 - Pants, $24.00
4 - Nationals Hat, $32.00
x - return to menu
""")
item = input("Please select from the menu or go back to the categories: ")
if item == '1' or item == '2' or item == '3':
#Adds item onto the the cart
cart.append(cloth_list[int(item)-1])
elif item == 'x':
Return = True
else:
print("Invalid input try again")
elif choice == '4':
while Return == False:
#Sub item menu
print("""
1 – Coho Salmon, $12.50
2 − Spaghetti, $2.75
3 – Milk, $3.99
4 – Eggs, $1.99
5 – Flat Tire Ale, $9.95
x - return to menu
""")
item = input("Please select from the menu or go back to the categories: ")
if item == '1' or item == '2' or item == '3' or item == '4' or item == '5':
#Adds item onto the the cart
cart.append(groc_list[int(item)-1])
elif item == 'x':
Return = True
else:
print("Invalid input try again")
elif choice == 'c':
checkout = True
print(cart)
else: print("Invalid input, please try again!")
else:
print("Do you want a new cart y/n")
choice = input()
if choice == 'y':
checkout = False
#Create new cart
cart.append([])
elif choice == 'n':
#Proceed to item summary
new_cart = False
else:
print("Invalid Option, Choose again")
#Print total number of carts
print("Total number of carts :",len(cart))
for v in range(len(cart)):
#Increment for each existing cart
print("Cart",v+1)
#Add total number of items within every cart
print("Total Number of items:",len(cart[v]))
#Add total price of items within every cart
print("Total cost of the items: $",sum(cart[v]))
main()
Your cart contains a list of items which themselves are array:
example, i ran your code and your cart looks like this:
[['My Own Words', 18.0], ['My Own Words', 18.0], ['My Own Words', 18.0], ['My Own Words', 18.0]]
you're trying to apply a sum on a position of an array, so for example when you do
sum(cart[0]) you call sum on ['My Own Words', 18.0] so your code tries to do:
'My Own Words'+18 which gives a type error.
if all you need is the just the total price you could just append the prices instead of the whole items, or you could simply append the prices to a seperate array and call sum on that
This is a shopping cart program where the user can specify which items to put in the cart and how many, they select items from a list. For example, they can add the movie Frozen to the cart for 3 units. Each item has a price associated with it which is specified in a dictionary called ItemPriceInv. I have to put all of these item prices in a list for the number of times the item appears in cart in order to calculate the sum. This is what i have so far, it will only add one occurence of price for each unique item in cart while i need it to list the prices of items for as many times as they appear in the cart:
class Cart():
#prints options to screen
print('''
1: Add an Item
2: Remove an Item
3: View Cart
4: View List
5: Checkout
6: Exit
''')
#dictionary of item prices and inventory
ItemPriceInv = {'The Bourne Identity': [9.99, 200], 'Harry Potter': [15.99,1000], 'The Holy Grail': [4.75, 800],
'Arrival': [24.99, 900], 'Hidden Figures': [29.98, 3], 'Fantastic Beastes': [11.99, 2000],
'Frozen': [19.99, 77], 'The Godfather':[10.99 ,55], 'Analyze This': [8.99 ,20],
'American Splendor':[3.99, 50], 'Lego Movie': [19.99, 233], 'Transformers': [24.99, 500],
'Limitless': [29.99, 2], 'The Matrix': [10.99, 278]}
cart= {}
cost = []
command = int(input("Enter the option you would like to execute: "))
while command != 6:
if command == 1: #code for 'add item' option
product = input("Enter the product you would like to add: ")
if product == 'cart':
print(cart)
if product in ItemPriceInv:
NumofProducts = int(input('Enter the number of this product you would like to add to cart: '))
if NumofProducts > ItemPriceInv[product][1]:
print("We do not carry that quantity of", product)
print(cart)
else:
cart[product] = NumofProducts
ItemPriceInv[product][1]= ItemPriceInv[product][1]- NumofProducts
if product not in ItemPriceInv:
print('We do not carry this product :( ')
elif command == 2: #code for option 2
product = input('Enter an item to remove: ')
NumofProducts = int(input('How many of this item do you want to remove? '))
cart[product]-= NumofProducts
print(cart)
elif command == 3:
print(cart)
elif command == 4:
print(ItemPriceInv)
elif command == 5:
ItemPriceList = []
for item in cart:
ItemPrice = ItemPriceInv[item][0]
ItemPriceList.append(ItemPrice)
print(ItemPriceList)
#ItemPriceList.append(ItemPrice) #prices of items in cart
#L = list(itertools.repeat([ItemPrice, NumofProducts]))
#for ItemPrice in ItemPriceList:
#ItemPriceList = [ItemPrice] * NumofProducts
elif command != 6:
print('please enter a valid option')
command = int(input('Enter the option you would like to execute: '))
else:
print('Cart closed')
···
elif command == 5:
ItemPriceList = []
for item in cart:
ItemPrice = ItemPriceInv[item][0]
ItemNum = cart[item]
ItemPriceList.extend([ItemPrice]*ItemNum)
print(ItemPriceList)
what I have to say is that follow a good code style please.
And the unused code that with # just do what you want.
i've got the code working but I've written my code in python implementing a stack, it is pushing and popping following LIFO but when you view the list it prints it as:
1
2
3
showing the last item which is 3 at the bottom, how can I make it show the last item at the top like a proper stack?
my code is as follows:
stack_pointer = 0
stack =[]
max_length = 2
def view():
for x in range (len(stack)):
print(stack[x])
def push():
global stack_pointer
if len (stack)> max_length:
print("Maximum stack length reached!")
else:
stack_pointer = stack_pointer + 1
item = input("Please enter the item you wishto add to the stack: ")
stack.append(item)
def pop():
global stack_pointer
if len (stack)<= 0:
print ("stack is empty!")
else:
item = stack.pop
stack_pointer = stack_pointer - 1
print ("you just popped out: ", item)
while True:
print ("")
print("Python implementation of a stack")
print("********************************")
print("1. view Stack")
print("2. Push onto Stack")
print("3. Pop out of Stack")
print("********************************")
print("")
menu_choice = int (input("Please enter your menu choice: "))
print ("")
print ("")
if menu_choice == 1:
view()
elif menu_choice == 2:
push()
elif menu_choice == 3:
pop()
Try something like this:
def view():
print(list(reversed(stack)))
to create reversed copy of your list and print it out.
This should work: a start of the stack's length - 1 , a stop of -1, and step of -1; no new lists are created or any extra operations, only a modification of the range object parameters, thus it's efficient:
def view():
for x in range (len(stack) - 1, -1, -1):
print(stack[x]) # or print(stack[x], end=' ')