-
Notifications
You must be signed in to change notification settings - Fork 6
/
1f_exercise_5_ice_cream_van_ANTIC.py
240 lines (219 loc) · 8.22 KB
/
1f_exercise_5_ice_cream_van_ANTIC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#14/05/2024 team ANTIC's Ice Cream Van with dataframes
#completed 15/05/2024 and fully functional
import numpy as np
import pandas as pd
import random
"""
Learning objectives
Functions *
Exception Handling *
File Handling () missing in this instance
NumPy (*) (only small amount for n-array)
Pandas (*)
"""
ascii_art = """
,---. ,--. ,--.,--------.,--. ,-----.,--.
/ O \ | ,'.| |'--. .--'| |' .--./| |,---.
| .-. || |' ' | | | | || | `-'( .-'
| | | || | ` | | | | |' '--'\ .-' `)
`--' `--'`--' `--' `--' `--' `-----' `----'
,--.
| | ,---. ,---. ,---.,--.--. ,---. ,--,--.,--,--,--. ,---.
| || .--'| .-. :| .--'| .--'| .-. :' ,-. || |( .-'
| |\ `--.\ --.\ `--.| | \ --.\ '-' || | | |.-' `)
`--' `---' `----' `---'`--' `----' `--`--'`--`--`--'`----'
"""
print(ascii_art)
####ice cream ascii_art source: https://ascii.co.uk/art/icecream
#### aside from the ice cream sandwich.
ascii_art_sundae = """
_
//
\,O. //
,(:::)=//
( `~(###)
%---'`"y
\ /
\ /
__)(__
'------`
"""
ascii_art_ice_cream = """
_
,' `,.
>-.(__)
(_,-' |
`. |
`.|
`
"""
ascii_art_whippy = """
()
(__)
(____)
(______)
(________)
(__________)
\/\/\/\/\/
\/\/\/\/
\/\/\/
\/\/
\/
"""
ascii_art_popcicle = """
_.-.
,'/ //\.
/// // /)
/// // //|
/// // ///
/// // ///
(`: // ///
`;`: ///
/ /:`:/
/ / `'
/ /
(_/
"""
ascii_art_ice_cream_sandwich = """
_____
/\ \ \.
/\/\ \ \*
/\/\/\ \ \.
/\/\/\/\__\_\.
/\/\/\/\/ /_/
/\/\/\/\/ /_/
\/\/\/\/ /_/
\/\/\/ /_/
\/\/ /_/
\/__/_/
"""
ascii_art_flake = """
() /z/
(__) /z/
(____)/z/
(_____/_/
(_____/_/)
(__________)
\/\/\/\/\/
\/\/\/\/
\/\/\/
\/\/
\/
"""
###import new icecream prices
#df = pd.read_csv(ice_cream_list.csv)
ice_cream_art_list = ["ascii_art_sundae", "ascii_art_popcicle",
"ascii_art_whippy","ascii_art_triple",
"ascii_art_ice_cream_sandwich", "ascii_art_flake"]
#ice cream selection and prices
ice_cream_selection = {"sundae": 2.49,
"popcicle":1.15,
"whippy": 0.99,
"triple": 3.50,
"ice_cream_sandwich":4.10,
"flake":1.10,
}
#turned dictionary into dataframe
df_icecreams = pd.DataFrame(ice_cream_selection.items(),
columns=['Ice_Creams', 'Cost_in_pound'])
###random number of ice cream in stock
df_icecreams["Stock"] = [random.randint(1, 15) for i in range(6)]
#Insert corresponding art into dataframe
df_icecreams["Art"] = ice_cream_art_list
#Ice cream van message
print("Today's Ice Cream menu: ")
print(df_icecreams[["Ice_Creams","Cost_in_pound"]])
#define function for transaction
def ice_cream_van(costumer_choice, money, number_icecream):
ice_cream_cost = df_icecreams.loc[df_icecreams['Ice_Creams'] == costumer_choice,
'Cost_in_pound'].item()
total_cost = (ice_cream_cost * number_icecream)
money_left = round(money - total_cost, 2)
if money_left < 0:
print(f"Sorry you do not have enough cash, you are {money_left} short")
else:
print("Thank you for your purchase")
return money_left
#while loops to repeat.
want_icecream = True
while want_icecream == True:
while True:
costumer_choice = input("What is the ice cream you want")
if (df_icecreams["Ice_Creams"]==costumer_choice).any() == True:
#checks if value in index
#costumer_choice in df_icecreams["Ice_Creams"]
#break to exit infinite loop
break
else:
print("Choose an icecream we have available:")
print(df_icecreams[["Ice_Creams","Cost_in_pound"]])
while True:
selected_ice_creams_left = df_icecreams.loc[df_icecreams \
["Ice_Creams"]==costumer_choice,["Stock"]].values[0].item()
#See if input is valid
try:
number_icecream = int(input("how many?"))
#if not valid raise exception and print out and loops back with while.
except:
print("Choose a number of ice creams you are buying.")
###additional choice features later.
#print("Would you like to buy them all or",
# " select another ice cream?")
#if input have no error
else:
#check for number of ice cream in stock for selected one,
#if enough continue
if df_icecreams.loc[df_icecreams["Ice_Creams"]==costumer_choice,
["Stock"]].values[0].item() >= number_icecream:
#exit loop
break
else:
print(f"Sorry, we have less than {selected_ice_creams_left} ",
"of ice creams in stock.")
print("Please select less than number of ice creams in stock")
while True:
try:
costumer_money = float(input("How much money do you have?"))
break
except ValueError:
print("Insert a number total you are spending")
try:
money_left = ice_cream_van(costumer_choice,
costumer_money, number_icecream)
if money_left < 0:
print("You do not have enough money for this choice, ",
"would you like to choose another iceream?")
#if enough money, retrieve art string from dataframe, convert to narray
#to singular value from narray value[first].item()
elif money_left >= 0:
print_art = df_icecreams.loc[df_icecreams["Ice_Creams"]
==costumer_choice,
["Art"]].values[0].item()
#print string from dataframe to call original variable, display art.
print(globals()[print_art])
#f-String to display values of previous choices and money left.
print(f"thank you for your purchase, and here is your "
f"{number_icecream} {costumer_choice} ")
print(f"and {money_left} pounds of change.")
#Update ice cream van's stock in pandas array for selected condition
df_icecreams.loc[df_icecreams["Ice_Creams"]==costumer_choice,
["Stock"]]-=number_icecream
#ask if you want to buy another one
ice_cream_choice = input("Type Y buy another ice cream : ")
if ice_cream_choice != "y" and ice_cream_choice != "Y":
want_icecream = False
print("Have a great time!")
break
except:
ice_cream_choice = input("Type Y to choose again : ")
if ice_cream_choice != "y" and ice_cream_choice != "Y":
want_icecream = False
print("Perhaps next time.")
break
#####Some other thoughts on what we could do
#other stuff to develop, create numpy array to represent cities,
#be a merchant to sell between places (different dimensions?)
#randomise prices etc.
##add random purchases?
##create a chart of how many ice creams are sold?
###purchase