Subtracting values w/list

Reply

Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Subtracting values w/list

 
0
  #1
Dec 18th, 2008
How do I subtract or add in a lists?
When I run the program below I get the message
print int(pool-strength)
TypeError: unsupported operand type(s) for -: 'list' and 'int'

  1. # Character Development Dungeons and Dragons
  2. # pool are the total points that I can use.
  3. pool = [30]
  4.  
  5. choice = None
  6.  
  7. while choice !="0":
  8. print \
  9. """
  10.  
  11. Character Development
  12.  
  13. 0 - Exit
  14. 1 - Strength
  15. """
  16.  
  17. choice = raw_input("Choice: ")
  18. print
  19. # exit
  20. if choice == "0":
  21. print "Good-bye."
  22.  
  23. # Strength points
  24. elif choice == "1":
  25. strength = 0
  26. strength = int(raw_input("How much strength points do you want?: "))
  27. print int(pool-strength)
  28.  
  29. # some unknown choice
  30. else:
  31. print "Sorry, but", choice, "isn't a valid choice."
  32.  
  33. raw_input("\n\nPress the enter key to exit.")
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 62
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Subtracting values w/list

 
0
  #2
Dec 18th, 2008
you are trying to do [30] - int,

perhaps you want to get the value from the list, not the list itself.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,202
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 342
woooee woooee is offline Offline
Nearly a Posting Virtuoso

Re: Subtracting values w/list

 
0
  #3
Dec 19th, 2008
It should be
pool = 30
The [] turn pool into a list and you want an integer variable.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Re: Subtracting values w/list

 
0
  #4
Dec 19th, 2008
Thanks. It works now with that change.
Just curious if I had left pool = [30] is it impossible to use operands for lists? Such as -, +, *, / if there is a way what is the correct method on doing so?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 662
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 112
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Subtracting values w/list

 
0
  #5
Dec 19th, 2008
Python lists support several methods for example: append, count, extend, index, insert, pop, remove, reverse, sort

http://www.letmegooglethatforyou.com/?q=Python+List
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 33
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: Subtracting values w/list

 
0
  #6
Dec 19th, 2008
Originally Posted by dseto200 View Post
Thanks. It works now with that change.
Just curious if I had left pool = [30] is it impossible to use operands for lists? Such as -, +, *, / if there is a way what is the correct method on doing so?
Yeah you can do that, but you have to specify which value in the list you're talking about. In this case you could do something like
pool[0] = pool[0] - 10 or
pool[0] -= 10.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 996 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2010 DaniWeb® LLC