Help With this code

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2007
Posts: 1,442
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Help With this code

 
0
  #1
Nov 24th, 2008
I enter first time and works fine, but second time? Boom, it fails!
What is wrong
Best regards:
  1. #Global values
  2. #key is ID/name and value is cost
  3. product = dict()
  4. identity = dict()
  5.  
  6. #Function to check and add product and its ID
  7. def get_prod():
  8. #anything can be a name
  9. prod_name = raw_input("Please Enter Product Name(Any name if don't remember)\n" "That requires you to remember ID : \n ")
  10. #filter out -ve and alphabet for ID
  11. try:
  12. a= int(raw_input("Please Enter ID \n" "Must be correct if name was not : \n "))
  13. if a>0: # a is not negative
  14. prod_id = a
  15. else:
  16. int("s") #rise exception purposely since s is string and cannot be used with int() function
  17. #exception message
  18. except:
  19. print "Error: Only Non negative integers for product ID"
  20.  
  21. #filter out -ve and alphabet for Number of Items
  22. try:
  23. b= int(raw_input("How many %s(s) do you need: \n" %(prod_name)))
  24. if b>0: # a is not negative
  25. prod_quant = b
  26. else:
  27. int("s") #rise exception purposely since s is string and cannot be used with int() function
  28. #exception message
  29. except:
  30. print "Error: Only Non negative integers for product ID"
  31. return (prod_name, prod_id, prod_quant)# Return tuple of three values
  32.  
  33.  
  34. # Function to update dictionaries
  35. def add_prod(prod_name, prod_id, prod_quant,prod_cost):
  36. t_cost = (float(prod_quant)*float(prod_cost))#cost =quantity x cost per product
  37. global product, identity
  38. product[prod_name] = t_cost
  39. identity[prod_id] = t_cost
  40.  
  41.  
  42. #function to get costs form the file and present arguments for above function
  43. # no error trapping though if somebody enters non existing value
  44. def prod_cost(prod_name, prod_id):
  45. fo = open('stock.txt', 'r')
  46.  
  47. prod_cost = 0 # intitialize to avod declar error
  48. for line in fo:
  49. a,b,c = line.split(",")# separate id, name, cost by comma
  50. if (b == prod_name or int(a) ==int(prod_id)) :
  51. prod_name = b
  52. prod_id = int(a)
  53. c_pro = c.strip("\n")#remove \n character from c
  54. prod_cost = (c_pro)
  55. return (prod_name, prod_id, prod_cost)
  56.  
  57.  
  58. #Main program calling functions to do a given task!!
  59. """ First we need to call a function to prompt user to enter values
  60. That function is "get_prod()"
  61. So we call it and it returns a tuple that have all values respectively (prod_name, prod_id, prod_quant)
  62. use while loop until custome says exit"""
  63.  
  64. while True:
  65. tuple1 = get_prod()
  66. name, ident, quant = tuple1
  67.  
  68. #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]
  69. # so we want to know cost, so we call prod_cost() passing args
  70. tuple2 = prod_cost(name, ident)
  71. prod_name, prod_id, prod_cost = tuple2
  72.  
  73. # Take all four arguments and use them in add_prod()
  74. add_prod(prod_name, prod_id, quant ,prod_cost)
  75. print product
  76. print identity
  77. exit = raw_input("Need to Add more? y/n:\n")
  78. if exit=="n":
  79. False
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 944
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Help With this code

 
0
  #2
Nov 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,442
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Help With this code

 
0
  #3
Nov 25th, 2008
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
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Help With this code

 
0
  #4
Nov 25th, 2008
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,442
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Help With this code

 
0
  #5
Nov 25th, 2008
Thanks Jimmy
That is grave mistake, and hard to detect
Well you detected it! heeeh thanks alot
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC