-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStocks and Prices.py
47 lines (36 loc) · 1.35 KB
/
Stocks and Prices.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
stocks = {
"info": [600, 630, 620],
"ril": [1430, 1490, 1567],
"mtl": [234, 180, 160]
}
userInput = input('''Enter Any one of the 2 commands
print - Prints all the stocks and prices
add - add a new stock and a new price
''')
print("\n")
def print_stocks(stock):
for stock_name, stock_price in stock.items():
average = round(sum(stock_price) / len(stock_price), 2)
print(f"{stock_name} ==> {stock_price} ==> avg: {average}")
def add(stock):
stock_name_input = input("Enter Stock name: ")
try:
stock_price_input = round(float(input("Enter Stock Price: ")))
if stock_name_input in stock:
stock[stock_name_input].append(stock_price_input)
print("\n")
print(f"Stock Name {stock_name_input} and Price {stock_price_input} Added Successfully!")
return print_stocks(stock)
else:
stock[stock_name_input] = [stock_price_input]
print("\n")
print(f"Stock Name {stock_name_input} and Price {stock_price_input} Added Successfully!")
return print_stocks(stock)
except ValueError:
print("Enter Numbers Only for Price")
if userInput.lower() == "print":
print_stocks(stocks)
elif userInput.lower() == "add":
add(stocks)
else:
print("Enter a valid command!")