Justin,
I watched your video. What do you think of my code. I did 4 passes. First with 3 arguments. Second with 2 sets of 3 arguments. Third with a 7th argument with error checking. Fourth with 8 arguments with error checking.
runbatch.txt.
python3 main.py carrots 4 .75
python3 main.py carrots 4 .75 peas 3 .52
python3 main.py carrots 4 .75 peas 3 .50 potatoes
python3 main.py carrots 4 .75 peas 3 .50 potatoes 4
#!/usr/bin/env python3
import sys
from sys import argv
filename = sys.argv[0]
arguments = sys.argv[1:]
str_count = len(arguments)
if str_count == 3:
print("using import sys")
item = arguments[0]
quantity = int(arguments[1])
cost = float(format(float(arguments[2]), '.2f'))
print(filename)
print(arguments)
total = quantity * cost
print("You are buying {} quantity {} @ ${} for ${}".format(item, quantity, format(cost, '.2f')
, format(total, '.2f')))
print("")
print("")
print("using from sys import argv")
print("using this method you drop sys from filename and arguments")
filename = argv[0]
arguments = argv[1:]
print(filename)
print(arguments)
cost = 0.00
total = 0.00
item = arguments[0]
quantity = int(arguments[1])
cost = float(format(float(arguments[2]), '.2f'))
total = quantity * cost
print("You are buying {} quantity {} @ ${} for ${}".format(item, quantity, format(cost, '.2f')
, format(total, '.2f')))
if str_count > 3:
print("")
print("")
print("we will see if we can parse off the arguments")
print(filename)
print(arguments)
print("I added in more arguments and will now parse them off")
print("")
print("")
count = 0
final_total = 0.00
subtotal = 0.00
while count < len(arguments):
item = arguments[count]
if count + 1 < len(arguments):
quantity = int(arguments[count + 1])
elif count + 1 >= len(arguments):
print("You didn't put in a quantity for {}".format(item))
break
if count + 2 < len(arguments):
cost = float(arguments[count + 2])
elif count + 2 >= len(arguments):
print("You didn't put in a cost for {}".format(item))
break
subtotal = quantity * cost
print("You are buying {} quantity {} @ ${} for ${}".format(item, quantity, format(cost, '.2f')
, format(subtotal, '.2f')))
count += 3
final_total += subtotal
print("Your final total is $", format(final_total, '.2f'))
here is the output.
./runbatch.txt
using import sys
main.py
['carrots', '4', '.75']
You are buying carrots quantity 4 @ $0.75 for $3.00
using from sys import argv
using this method you drop sys from filename and arguments
main.py
['carrots', '4', '.75']
You are buying carrots quantity 4 @ $0.75 for $3.00
we will see if we can parse off the arguments
main.py
['carrots', '4', '.75', 'peas', '3', '.52']
I added in more arguments and will now parse them off
You are buying carrots quantity 4 @ $0.75 for $3.00
You are buying peas quantity 3 @ $0.52 for $1.56
Your final total is $ 4.56
we will see if we can parse off the arguments
main.py
['carrots', '4', '.75', 'peas', '3', '.50', 'potatoes']
I added in more arguments and will now parse them off
You are buying carrots quantity 4 @ $0.75 for $3.00
You are buying peas quantity 3 @ $0.50 for $1.50
You didn't put in a quantity for potatoes
Your final total is $ 4.50
we will see if we can parse off the arguments
main.py
['carrots', '4', '.75', 'peas', '3', '.50', 'potatoes', '4']
I added in more arguments and will now parse them off
You are buying carrots quantity 4 @ $0.75 for $3.00
You are buying peas quantity 3 @ $0.50 for $1.50
You didn't put in a cost for potatoes
Your final total is $ 4.50