Skip to content

Commit e28210a

Browse files
authored
Merge pull request #107 from lironmiz/unit7-Ex7.2.6
Create unit7_ex7.2.6.py
2 parents ba0ec7c + 87b9ebd commit e28210a

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Diff for: unit7_ex7.2.6.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# exercise 7.2.6 from unit 7
2+
'''
3+
Write a program that receives from the user a single string representing a list of products
4+
for shopping, separated by commas without spaces.
5+
An example of an input string: "Milk,Cottage,Tomatoes".
6+
7+
The program asks the user to key in a number (digit) in the range of one to nine
8+
(there is no need to check the correctness of the input).
9+
Depending on the number received, perform one of the following actions, according to
10+
the following breakdown:
11+
12+
The program asks the user to key in a number (digit) in the range of one to nine
13+
(there is no need to check the correctness of the input).
14+
Depending on the number received, perform one of the following actions, according
15+
to the following breakdown:
16+
17+
1.Printing the list of products
18+
2.Printing the number of products in the list
19+
3.Printing the answer to the test "Is the product on the list?" (The user will be asked to enter a product name)
20+
4.Printing the answer to the test "How many times does a certain product appear?" (The user will be asked to enter a product name)
21+
5.Deleting a product from the list (the user will be asked to tap a product name, only one product will be deleted)
22+
6.Adding a product to the list (the user will be asked to tap a product name)
23+
7.Printing all invalid products (a product is invalid if its length is less than 3 or it contains characters other than letters)
24+
8.Removing all existing duplicates in the list
25+
26+
Output
27+
28+
Please note, after the user makes a selection, the user will return to the main menu until he
29+
selects the exit (he will press the number 9).
30+
31+
Guidelines
32+
Transfer the products that the program accepts to the list.
33+
Use additional functions of your choice.
34+
35+
'''
36+
37+
def main():
38+
products = input("Enter a string of products separated by commas: ").split(",")
39+
40+
while True:
41+
print("Menu:")
42+
print("1. Print the list of products")
43+
print("2. Print the number of products in the list")
44+
print("3. Check if a product is on the list")
45+
print("4. Count the number of occurrences of a product")
46+
print("5. Delete a product from the list")
47+
print("6. Add a product to the list")
48+
print("7. Print invalid products")
49+
print("8. Remove duplicates from the list")
50+
print("9. Exit")
51+
52+
choice = int(input("Enter your choice 1-9: "))
53+
if choice == 1:
54+
print(products)
55+
elif choice == 2:
56+
print(len(products))
57+
elif choice == 3:
58+
product = input("Enter a product name: ")
59+
if product in products:
60+
print("The product is on the list.")
61+
else:
62+
print("The product is not on the list.")
63+
elif choice == 4:
64+
product = input("Enter a product name: ")
65+
print(products.count(product))
66+
elif choice == 5:
67+
product = input("Enter a product name: ")
68+
products.remove(product)
69+
elif choice == 6:
70+
product = input("Enter a product name: ")
71+
products.append(product)
72+
elif choice == 7:
73+
invalid_products = [p for p in products if len(p) < 3 or not p.isalpha()]
74+
print(invalid_products)
75+
elif choice == 8:
76+
products = list(set(products))
77+
elif choice == 9:
78+
break
79+
80+
if __name__ == "__main__":
81+
main()

0 commit comments

Comments
 (0)