Skip to content

Instantly share code, notes, and snippets.

@roman-on
Created April 28, 2020 20:13
Show Gist options
  • Save roman-on/614a3002f8c1be35ae902f96d0a71f57 to your computer and use it in GitHub Desktop.
Save roman-on/614a3002f8c1be35ae902f96d0a71f57 to your computer and use it in GitHub Desktop.
"""
Write a program that receives from the user one string that represents
a product list for shopping, separated by commas without any spaces.
Example of an input string: "Milk, Cottage, Tomatoes".
The program asks the user to enter a number (digit) in one to nine ranges (no input validity is required).
Depending on the number received, do one of the following:
1. Print the product list
2. Prints the number of products in the list
3. Print the answer to the check "Is the product on the list?" (User will be asked to enter a product name)
4. Print the test answer "How many times does a particular product appear?" (User will be asked to enter a product name)
5. Delete a product from the list (the user will be asked to enter a product name, only one product will be deleted)
6. Add a product to the list (the user will be asked to enter a product name)
7. Print all invalid products (product is invalid if its length is less than 3 or includes non-letter characters)
8. Removes all duplicates in the list
9. Output
Note, after making a user selection, the user will return to the main menu until he selects the port (press number 9).
Instructions:
Move the products that the program receives to the list.
Use additional functions to choose from.
 """
# Press "0" just for the first time to start the loop:
press_num = int(input("Press a starting number:"))
# or you can just enter this to start
press_num = 0
# or you have to enter this to start
shop_lower_list = Shopping (0)
# choose a ready "shop_list" from ABOVE
# I MADE THIS LIST TO CHECK ALL STEPS: Adding this only one time. im not allow to put it inside the loop
shop_list = "#$#@$,eg,Milk,Cottage,Tomatoes,milk,milk,milk,eggs,eggs,eggs"
""" Write a program that receives from the user one string that represents
a product list for shopping, separated by commas WITHOUT ANY SPACES. """
def Shopping (press_num):
list_ = [] # new variable for a list
shop_lower = shop_list.lower() # Make my string "shop list" a lower characters
shop_lower_list = shop_lower.split(",") # Converting the string "shop lower" to a list and saving in a new value
while (press_num < 9): # Using the while loop in order to make the pro discontinues till pressing exit num 9
press_num = int(input("Shopping list press a number between 1 - 8 or 9 to EXIT:"))
if press_num == 1:
print ("The product list:") # Print the product list
print (shop_lower_list)
elif press_num == 2:
shop_new = shop_list.replace(",", " ") # Replacing the commas by space
print ("The number of products in the list:")
print (len(shop_new.split())) # Printing the number of products in the list
elif press_num == 3:
print ("Does the product on the list?") # Printing the question - Is the product on the list?
prod_name = input("Enter a product name: ").lower() # Asking the user to input the product name in order to check if the product in the list
if prod_name in shop_list.lower(): # Checking if there is such a product and if yes so it's returns the msg
print ("Yep")
if prod_name not in shop_list.lower(): # If this list doesnt contain this product it's printing another msg
print ("There is no such a product on the list.")
elif press_num == 4:
print ("How many times does a particular product appear?")
prod_name = (input("Enter a product name: ")).lower() # Asking the user to input the product name
print (shop_lower_list.count(prod_name)) # Counting how many times the product that entered by the user apears in the shopping list
elif press_num == 5:
print ("Delete one product from the list.") # Printing the msg what is it going to do
prod_name = (input("Enter a product name: ")).lower() # Asking the user to input the product name
shop_lower_list.remove(prod_name) # Removing one product that was entered from the list
print (shop_lower_list) # Printing a new list with the products that was left in the list
elif press_num == 6:
print ("Add a product to the list.") # Printing the msg what is it going to do
prod_name = (input("Enter a product name: ")).lower() # Asking the user to input the product name
shop_lower_list.append(prod_name) # Adding a product to the list
print (shop_lower_list) # Printing an updated list with the products that was in the list plus a new one that was entered
elif press_num == 7:
print("Printing all invalid products:") # Printing the msg what is it going to do
for product in shop_lower_list: # Using "for" function to run through the product list
if len(product) < 3 or not(product.isalpha()): # Make a condition: product is invalid if its length is less than 3 or includes non-letter characters
list_.append(product) # Adding all invalid products to the list
print (list_) # Printing the list of invalid products
elif press_num == 8:
print ("Removes all duplicates in the list:") # Printing the msg what is it going to do
for product in shop_lower_list: # Using "for" command to check the duplicates in the list
if not (product in list_): # Condition - if the product not in the list so add it
list_.append(product) # Adding the product to a new list
shop_lower_list = list_ # Moving the result to the original list
print (shop_lower_list) # Returning the new list
if press_num == 9:
print ("Exit")
return shop_lower_list
break # Exit the loop
def main():
<your code here>
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment