Skip to content

My Shopping python program #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Kim-Venom opened this issue May 29, 2022 · 5 comments
Open

My Shopping python program #1521

Kim-Venom opened this issue May 29, 2022 · 5 comments

Comments

@Kim-Venom
Copy link

#a shopping program that sells add and check out to customers

import time
import os

shopping_cart = 0

total_price = 0

item_list = []

#shopping function

def shopping ():
global shopping_cart
global total_price

print ("Here are lists of items for sale: ")
print ("1. Apple - $3")
print ("2. Soap - $6")
print ("3. Soda - $4")
print ("4. Pie - $8")
print ("5. Cake - $14 ")
print ("6. Exit \n")

while shopping_cart < 20:
    ch = int(input("Enter the numerical value of the goods you like to purchase: "))

    if ch == 1:
        print ("You have added Apple to you cart")
        shopping_cart = shopping_cart + 1
        total_price = total_price + 3
        item_list.append(" - Apple -")
        print ("You have " + str(shopping_cart) + " item/s  on your shopping cart")
        time.sleep (3)
        os.system('clear')
        print ("Here are lists of items for sale: ")
        print ("1. Apple - $3")
        print ("2. Soap - $6")
        print ("3. Soda - $4")
        print ("4. Pie - $8")
        print ("5. Cake - $14 ")
        print ("6. Exit \n")
    elif ch == 2:
        print ("You have added Soap to you cart")
        shopping_cart = shopping_cart + 1
        total_price = total_price + 6
        item_list.append(" - Soap -")
        print ("You have " + str(shopping_cart) + " item/s  on your shopping cart")
        time.sleep (3)
        os.system('clear')
        print ("Here are lists of items for sale: ")
        print ("1. Apple - $3")
        print ("2. Soap - $6")
        print ("3. Soda - $4")
        print ("4. Pie - $8")
        print ("5. Cake - $14 ")
        print ("6. Exit \n")
  
    elif ch == 3:
        print ("You have added Soda to your cart")
        shopping_cart = shopping_cart + 1
        total_price = total_price + 4
        item_list.append(" - Soda -")
        print ("You have " + str(shopping_cart) + " item/s  on your shopping cart")
        time.sleep (3)
        os.system('clear')
        print ("Here are lists of items for sale: ")
        print ("1. Apple - $3")
        print ("2. Soap - $6")
        print ("3. Soda - $4")
        print ("4. Pie - $8")
        print ("5. Cake - $14 ")
        print ("6. Exit \n")
  
    elif ch == 4:
        print ("You have added Pie to you cart")
        shopping_cart = shopping_cart + 1
        total_price = total_price + 8
        item_list.append(" - Pie -")
        print ("You have " + str(shopping_cart) + " item/s  on your shopping cart")
        time.sleep (3)
        os.system('clear')
        print ("Here are lists of items for sale: ")
        print ("1. Apple - $3")
        print ("2. Soap - $6")
        print ("3. Soda - $4")
        print ("4. Pie - $8")
        print ("5. Cake - $14 ")
        print ("6. Exit \n")
        
    elif ch == 5:
        print ("You have added Cake to your cart")
        shopping_cart = shopping_cart + 1
        total_price = total_price + 14
        item_list.append(" - Cake -")
        print ("You have " + str(shopping_cart) + " item/s  on your shopping cart")
        time.sleep (3)
        os.system('clear')
        print ("Here are lists of items for sale: ")
        print ("1. Apple - $3")
        print ("2. Soap - $6")
        print ("3. Soda - $4")
        print ("4. Pie - $8")
        print ("5. Cake - $14 ")
        print ("6. Exit \n")
    elif ch == 6:
        print ("Thank you for Shopping with us...")
        print("Below is your item lists in your cart: \n")
        print (item_list)
        print ("\nBelow is the total amount for all goods: \n")
        print("$" + str(total_price))
        exit ()

    else:
        print ("Invalid input Try again: ")
        time.sleep (3)
        os.system('clear')
        shopping()
        

print ("Thank you for Shopping with us..., your cart is full")
print("Below is your item lists in your cart: \n")
print (item_list)
print ("\nBelow is the total amount for all goods: \n")
print("$" + str(total_price))
exit ()

shopping()

@Kim-Venom
Copy link
Author

Please you can correct and lecture me on some stuffs am a beginner

@akjroller
Copy link
Contributor

akjroller commented May 29, 2022

@Kim-Venom Here are a few changes that I would make, but I'm also newer to python. I added f strings to make it easier to read and write the code, I also was able to get rid of the need to be so redundant with adding the print statements. I did remove the os.clear because I could not see why it was needed, but you can add it back in if needed. Hope this helps some. If the f strings don't work for you then you are using python 2 and should uninstall it and start using python 3 as python 2 is at end of life and not under active dev anymore.

import time
 
 shopping_cart = 0
 total_price = 0
 item_list = []
 
 
 def shopping():
     global shopping_cart
 
 
 while shopping_cart < 20:
     print("Here are lists of items for sale: ")
     print("1. Apple - $3")
     print("2. Soap - $6")
     print("3. Soda - $4")
     print("4. Pie - $8")
     print("5. Cake - $14 ")
     print("6. Exit \n")
     ch = int(input("Enter the numerical value of the goods you like to purchase: "))
 
     if ch == 1:
         item = 'Apple'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 3
         item_list.append(f"{item}")
         print(f"You're total price so far is {total_price}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 2:
         item = 'Soap'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 6
         item_list.append(f"{item}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 3:
         item = 'Soda'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 4
         item_list.append(f"{item}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 4:
         item = 'Pie'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 8
         item_list.append(f"{item}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 5:
         item = 'Cake'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 14
         item_list.append(f"{item}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 6:
         print("Thank you for Shopping with us..., your cart is full")
         print("Below is your item lists in your cart: \n")
         print(*item_list)
         print("\nBelow is the total amount for all goods: \n")
         print(f"${total_price}")
         exit()
     else:
         print("Invalid input Try again: ")
         time.sleep(3)
         # removed the OS clear and added continue here
         continue
shopping()

@Kim-Venom
Copy link
Author

@Kim-Venom Here are a few changes that I would make, but I'm also newer to python. I added f strings to make it easier to read and write the code, I also was able to get rid of the need to be so redundant with adding the print statements. I did remove the os.clear because I could not see why it was needed, but you can add it back in if needed. Hope this helps some. If the f strings don't work for you then you are using python 2 and should uninstall it and start using python 3 as python 2 is at end of life and not under active dev anymore.

import time
 
 shopping_cart = 0
 total_price = 0
 item_list = []
 
 
 def shopping():
     global shopping_cart
 
 
 while shopping_cart < 20:
     print("Here are lists of items for sale: ")
     print("1. Apple - $3")
     print("2. Soap - $6")
     print("3. Soda - $4")
     print("4. Pie - $8")
     print("5. Cake - $14 ")
     print("6. Exit \n")
     ch = int(input("Enter the numerical value of the goods you like to purchase: "))
 
     if ch == 1:
         item = 'Apple'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 3
         item_list.append(f"{item}")
         print(f"You're total price so far is {total_price}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 2:
         item = 'Soap'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 6
         item_list.append(f"{item}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 3:
         item = 'Soda'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 4
         item_list.append(f"{item}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 4:
         item = 'Pie'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 8
         item_list.append(f"{item}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 5:
         item = 'Cake'
         print(f"You have added {item} to you cart")
         shopping_cart = shopping_cart + 1
         total_price = total_price + 14
         item_list.append(f"{item}")
         print(f"You have {shopping_cart} item/s  on your shopping cart")
     elif ch == 6:
         print("Thank you for Shopping with us..., your cart is full")
         print("Below is your item lists in your cart: \n")
         print(*item_list)
         print("\nBelow is the total amount for all goods: \n")
         print(f"${total_price}")
         exit()
     else:
         print("Invalid input Try again: ")
         time.sleep(3)
         # removed the OS clear and added continue here
         continue
shopping()

Thanks so much I really appreciate it

@akjroller
Copy link
Contributor

akjroller commented May 29, 2022

@Kim-Venom
No problem, glad I could help. If you ever have any questions let me know and I'll assist if I'm able.
You can also go to my repository and take a look at some small python projects for beginners. Here is a link to it.
You can also check out my YouTube Channel here I am about to start a python programming tutorial series.

@sundaram2021
Copy link
Contributor

sundaram2021 commented Sep 10, 2022

made function "details" of repeating codes

#removed "shopping" and globalisation of the value of shopping_cart

import time

shopping_cart = 0
total_price = 0
item_list = []

def details(element, itemPrice, noOfItemInShoppingCart):
print(f"You have added {element} to you cart")
item_list.append(f"{element}")
print(f"You're total price so far is ${itemPrice}")
print(f"You have {noOfItemInShoppingCart} item/s on your shopping cart")

while shopping_cart < 20:
print("Here are lists of items for sale: ")
print("1. Apple - $3")
print("2. Soap - $6")
print("3. Soda - $4")
print("4. Pie - $8")
print("5. Cake - $14")
print("6. Exit \n")
ch = int(input("Enter the numerical value of the goods you like to purchase: "))

if ch == 1:
  shopping_cart += 1
  total_price += 3
  details("Apple", total_price, shopping_cart);
elif ch == 2:
  shopping_cart += 1
  total_price += 6
  details("Soap", total_price, shopping_cart);
elif ch == 3:
  shopping_cart += 1
  total_price += 4
  details("Soda", total_price, shopping_cart);
elif ch == 4:
  shopping_cart += 1
  total_price += 8
  details("Pie", total_price, shopping_cart);
elif ch == 5:
  shopping_cart += 1
  total_price += 14
  details("Cake", total_price, shopping_cart);
elif ch == 6:
  print("Thank you for Shopping with us..., your cart is full")
  print("Item lists in your cart: \n")
  print(*item_list)
  print(f"\nBelow is the total amount for all goods: ${total_price} \n")
  exit()
else:
  print("Invalid input Try again: ")
  time.sleep(3)
  # removed the OS clear and added continue here
  continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants