1
+ items = {"apple" :5 ,"soap" :4 ,"soda" :6 ,"pie" :7 ,"cake" :20 }
2
+ total_price = 0
3
+ try :
4
+ print ("""
5
+ Press 1 for apple
6
+ Press 2 for soap
7
+ Press 3 for soda
8
+ Press 4 for pie
9
+ Press 5 for cake
10
+ Press 6 for bill""" )
11
+ while True :
12
+ choice = int (input ("enter your choice here..\n " ))
13
+ if choice == 1 :
14
+ print ("Apple added to the cart" )
15
+ total_price += items ["apple" ]
16
+
17
+ elif choice == 2 :
18
+ print ("soap added to the cart" )
19
+ total_price += items ["soap" ]
20
+ elif choice == 3 :
21
+ print ("soda added to the cart" )
22
+ total_price += items ["soda" ]
23
+ elif choice == 4 :
24
+ print ("pie added to the cart" )
25
+ total_price += items ["pie" ]
26
+ elif choice == 5 :
27
+ print ("cake added to the cart" )
28
+ total_price += items ["cake" ]
29
+ elif choice == 6 :
30
+ print (f"""
31
+
32
+ Total amount :{ total_price }
33
+ """ )
34
+ break
35
+ else :
36
+ print ("Please enter the digits within the range 1-6.." )
37
+ except :
38
+ print ("enter only digits" )
39
+
40
+ """
41
+ Code Explanation:
42
+ A dictionary named items is created to store product names and their corresponding prices.
43
+ Example: "apple": 5 means apple costs 5 units.
44
+
45
+ one variable is initialized:
46
+
47
+ total_price to keep track of the overall bill.
48
+
49
+
50
+ A menu is printed that shows the user what number to press for each item or to generate the final bill.
51
+
52
+ A while True loop is started, meaning it will keep running until the user explicitly chooses to stop (by selecting "6" for the bill).
53
+
54
+ Inside the loop:
55
+
56
+ The user is asked to enter a number (1–6).
57
+
58
+ Depending on their input:
59
+
60
+ If they enter 1–5, the corresponding item is "added to the cart" and its price is added to the total_price.
61
+
62
+ If they enter 6, the total price is printed and the loop breaks (ends).
63
+
64
+ If they enter something outside 1–6, a warning message is shown.
65
+
66
+ The try-except block is used to catch errors if the user enters something that's not a number (like a letter or symbol).
67
+ In that case, it simply shows: "enter only digits".
68
+ """
0 commit comments