-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_methods_practice.py
More file actions
53 lines (43 loc) · 1.43 KB
/
list_methods_practice.py
File metadata and controls
53 lines (43 loc) · 1.43 KB
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
# list methods practice activity
import random
# PART 1
items = []
prices = []
item = input("First item (leave blank to stop): ").lower()
while item:
price = float(input("Price: "))
items.append(item)
prices.append(price)
item = input("Next item (leave blank to stop): ").lower()
# PART 2
print("\nItems:\t\tPrices:") # table header
for i in range(len(prices)): # loop through range of indices
print("{}\t\t${:.2f}".format(items[i], prices[i]))
# PART 3
rand_item = random.choice(items)
rand_index = items.index(rand_item)
prices[rand_index] = prices[rand_index] * .5
# alternate way
'''
index_count = 0
for i in items:
if rand_item == i: # find the random item
rand_index = index_count # save the random item’s index
# or, rand_index = items.index(rand_item)
prices[rand_index] = prices[rand_index] * .5 # update price
index_count += 1
'''
print("\nFLASH SALE! A random item has been discounted by 50%: Your {} is now ${:.2f}.".format(items[rand_index], prices[rand_index]))
# PART 4
print("\n", items)
index_count = 0
for i in range(len(items)):
if "eggs" == items[i]: # find the eggs
removed = items.pop(i) # remove eggs
prices.pop(i) # remove eggs price
print("The", removed, "have been removed.")
print(items)
# alternate way
#eggs_index = items.index(“eggs”)
#items.pop(eggs_index)
#prices.pop(eggs_index)