|
1 | 1 | ''' |
| 2 | +Grocery List Creator Python program |
| 3 | +
|
| 4 | +Created by Joseph C. Richardson |
| 5 | +
|
2 | 6 | This is a full functional grocery list Python program example to add to my Python book. |
3 | 7 | Feel free to copy this Python program example if you like. See what you can do with it. |
4 | | -As time rolls on, I will make this an actual, working program, which the user can place |
5 | | -the price of a grocery list item, while it keeps track of what you will be spending at the |
6 | | -store. The user will also be able to clear the list items or clear just one single list item, |
7 | | -while the price part of the program will work as addition and subtraction of list pricing, |
8 | | -via items added or removed from the shopping list. You are all welcome to copy this |
9 | | -program and make it do and be anything you like. Maybe try and create the list, such as |
10 | | -what I want to make it do down the road. Please enjoy. |
| 8 | +Please enjoy. |
11 | 9 | ''' |
12 | 10 | import os |
13 | 11 |
|
14 | | -title='title Grocery List Creator' |
| 12 | +text_colours=( |
| 13 | + '\x1b[31m', # index 0 = red |
| 14 | + '\x1b[32m', # index 1 = green |
| 15 | + '\x1b[33m', # index 2 = yellow |
| 16 | + '\x1b[34m', # index 3 = blue |
| 17 | + '\x1b[35m', # index 4 = purple |
| 18 | + '\x1b[36m', # index 5 = cyan |
| 19 | + '\x1b[37m' # index 6 = white |
| 20 | + ) |
| 21 | + |
| 22 | +title='title grocery list creator' |
15 | 23 | clear_screen='cls' |
16 | 24 | single_line_break='\n' |
17 | 25 | double_line_break='\n\n' |
18 | 26 | indent=' '*2 |
19 | 27 | dot_space='.'*3 |
20 | 28 | create_list='vgl' |
| 29 | +enter=0 |
| 30 | +decimal_point=100 |
21 | 31 | button1='y' |
22 | 32 | button2='n' |
23 | | -enter='' |
24 | | - |
25 | | -os.system(title) |
26 | 33 |
|
27 | 34 | sentence=[ |
28 | | -f'{single_line_break}{indent}Grocery List Creator', # index 0 |
| 35 | +f'{single_line_break}{indent}grocery list creator', # index 0 |
29 | 36 |
|
30 | 37 | f'''{double_line_break}{indent}Press "Enter" after each grocery list item typed. |
31 | 38 | {indent}Type "vgl", then press "Enter" to view your grocery list output.''', # index 1 |
32 | 39 |
|
33 | 40 | f'{double_line_break}{indent}Please type your grocery list items: ', # index 2 |
34 | 41 |
|
35 | | -f'{single_line_break}{indent}You have', # indext 3 |
| 42 | +f'{double_line_break}{indent}Please type your grocery list item price: $', # index 3 |
| 43 | + |
| 44 | +f'{double_line_break}{indent}You have', # indext 4 |
36 | 45 |
|
37 | | -'items in your grocery list.', # index 4 |
| 46 | +'items in your grocery list.', # index 5 |
38 | 47 |
|
39 | | -f'{double_line_break}{indent}Here is your grocery list output:', # index 5 |
| 48 | +f'{double_line_break}{indent}Here is your grocery list output:', # index 6 |
| 49 | + |
| 50 | +f'{single_line_break}{indent}Your grocery list total:', # index 7 |
40 | 51 |
|
41 | 52 | f'''{single_line_break}{indent}Do you wish to add more items to your grocery list? |
42 | | -{indent}Press "y" or "n" to confirm: ''' # index 6 |
| 53 | +{single_line_break}{indent}Press "y" or "n" to confirm: ''', # index 8 |
| 54 | + |
| 55 | +f'{single_line_break}{indent}thank you for choosing grocery list creator... \ |
| 56 | +press enter to exit.', # index 9 |
43 | 57 | ] |
44 | 58 |
|
45 | | -user_data=set() |
46 | | -user_data.add(enter) |
| 59 | +user_input_item_data=[ ] |
| 60 | +user_input_price_data=[ ] |
| 61 | + |
| 62 | +user_input_item_data.append(enter) |
| 63 | +user_input_price_data.append(enter) |
| 64 | + |
| 65 | +os.system(title.title()) |
47 | 66 |
|
48 | 67 | def items_list(): |
| 68 | + |
49 | 69 | while True: |
50 | 70 | os.system(clear_screen) |
51 | | - grocery_list=input(sentence[0]\ |
| 71 | + grocery_list=input(text_colours[5]+sentence[0].title() |
52 | 72 | +sentence[1]+sentence[2]).strip() |
53 | | - user_data.add(grocery_list) |
| 73 | + user_input_item_data.append(grocery_list) |
| 74 | + |
54 | 75 | if grocery_list==create_list: |
55 | | - user_data.remove(create_list) |
56 | | - convert=list(user_data) |
57 | | - convert.remove(enter) |
58 | | - convert.sort() |
| 76 | + user_input_item_data.remove(create_list) |
| 77 | + item=user_input_item_data |
| 78 | + item.remove(enter) |
59 | 79 | break |
60 | 80 |
|
| 81 | + try: |
| 82 | + os.system(clear_screen) |
| 83 | + item_price=float(input(text_colours[5]+sentence[0].title() |
| 84 | + +sentence[1]+sentence[3]).strip()) |
| 85 | + user_input_price_data.append(item_price) |
| 86 | + except ValueError: |
| 87 | + user_input_price_data.append(0) |
| 88 | + |
61 | 89 | while True: |
62 | 90 | os.system(clear_screen) |
63 | | - print(sentence[0]+sentence[5]) |
64 | | - |
65 | | - x=1 |
66 | | - for i in convert: |
67 | | - print(f'{single_line_break}{indent}{x}) {dot_space}',i.title()) |
68 | | - x+=1 |
69 | | - |
70 | | - print(sentence[3],len(convert),sentence[4]) |
71 | | - grocery_list=input(sentence[6]).lower().strip() |
| 91 | + print(sentence[0].title()+sentence[6]) |
| 92 | + |
| 93 | + try: |
| 94 | + x=1 |
| 95 | + for i in item: |
| 96 | + print(f'{single_line_break}{indent}{x}) {dot_space} {i} \ |
| 97 | +${user_input_price_data[x]/decimal_point}'.title()) |
| 98 | + x+=1 |
| 99 | + except IndexError: |
| 100 | + item.remove(enter) |
| 101 | + |
| 102 | + total_sum=user_input_price_data |
72 | 103 |
|
| 104 | + print(f'{sentence[7]} ${sum(total_sum)/decimal_point}', |
| 105 | + sentence[4],len(item),sentence[5]) |
| 106 | + |
| 107 | + grocery_list=input(sentence[8]).lower().strip() |
| 108 | + |
| 109 | + user_input_item_data.append(enter) |
| 110 | + |
73 | 111 | if grocery_list==button1: |
74 | | - items_list() |
| 112 | + items_list() |
75 | 113 | break |
76 | 114 | elif grocery_list==button2: |
77 | 115 | break |
78 | | - |
| 116 | + |
79 | 117 | items_list() |
80 | | -input('end') |
| 118 | +os.system(clear_screen) |
| 119 | +input(sentence[9].title()) |
0 commit comments