I enter first time and works fine, but second time? Boom, it fails!
What is wrong
Best regards:
#Global values
#key is ID/name and value is cost
product = dict()
identity = dict()
#Function to check and add product and its ID
def get_prod():
#anything can be a name
prod_name = raw_input("Please Enter Product Name(Any name if don't remember)\n" "That requires you to remember ID : \n ")
#filter out -ve and alphabet for ID
try:
a= int(raw_input("Please Enter ID \n" "Must be correct if name was not : \n "))
if a>0: # a is not negative
prod_id = a
else:
int("s") #rise exception purposely since s is string and cannot be used with int() function
#exception message
except:
print "Error: Only Non negative integers for product ID"
#filter out -ve and alphabet for Number of Items
try:
b= int(raw_input("How many %s(s) do you need: \n" %(prod_name)))
if b>0: # a is not negative
prod_quant = b
else:
int("s") #rise exception purposely since s is string and cannot be used with int() function
#exception message
except:
print "Error: Only Non negative integers for product ID"
return (prod_name, prod_id, prod_quant)# Return tuple of three values
# Function to update dictionaries
def add_prod(prod_name, prod_id, prod_quant,prod_cost):
t_cost = (float(prod_quant)*float(prod_cost))#cost =quantity x cost per product
global product, identity
product[prod_name] = t_cost
identity[prod_id] = t_cost
#function to get costs form the file and present arguments for above function
# no error trapping though if somebody enters non existing value
def prod_cost(prod_name, prod_id):
fo = open('stock.txt', 'r')
prod_cost = 0 # intitialize to avod declar error
for line in fo:
a,b,c = line.split(",")# separate id, name, cost by comma
if (b == prod_name or int(a) ==int(prod_id)) :
prod_name = b
prod_id = int(a)
c_pro = c.strip("\n")#remove \n character from c
prod_cost = (c_pro)
return (prod_name, prod_id, prod_cost)
#Main program calling functions to do a given task!!
""" First we need to call a function to prompt user to enter values
That function is "get_prod()"
So we call it and it returns a tuple that have all values respectively (prod_name, prod_id, prod_quant)
use while loop until custome says exit"""
while True:
tuple1 = get_prod()
name, ident, quant = tuple1
#Our add function needs four arguments but takes two args and ruturns three [the fourth is unused one among those returned by get_prod() i.e prod_quantity]
# so we want to know cost, so we call prod_cost() passing args
tuple2 = prod_cost(name, ident)
prod_name, prod_id, prod_cost = tuple2
# Take all four arguments and use them in add_prod()
add_prod(prod_name, prod_id, quant ,prod_cost)
print product
print identity
exit = raw_input("Need to Add more? y/n:\n")
if exit=="n":
False