| | |
Help With this code
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I enter first time and works fine, but second time? Boom, it fails!
What is wrong
Best regards:
What is wrong
Best regards:
python Syntax (Toggle Plain Text)
#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
Could we have an example of what stock.txt has in it please?
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
Sorry I forgot to attach it, and until I got home...anyway it have something like
001, pepsi, 9.00
002, coca, 18.00
Like simple database for comparing with user input to get price
001, pepsi, 9.00
002, coca, 18.00
Like simple database for comparing with user input to get price
Your problem is that you've got prod_cost as both a function and a variable. So once your assigning a value to prod_cost, it removed the definition of prod_cost as a function.
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the Python Forum
- Previous Thread: Word Jumble python program
- Next Thread: help with this program
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyqt python random recursion recursive refresh schedule screensaverloopinactive script scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






