943,866 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1682
  • Python RSS
Dec 18th, 2008
0

Subtracting values w/list

Expand Post »
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'

python Syntax (Toggle Plain Text)
  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.")
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dseto200 is offline Offline
56 posts
since Nov 2008
Dec 19th, 2008
0

Re: Subtracting values w/list

you are trying to do [30] - int,

perhaps you want to get the value from the list, not the list itself.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Dec 19th, 2008
0

Re: Subtracting values w/list

It should be
pool = 30
The [] turn pool into a list and you want an integer variable.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Dec 19th, 2008
0

Re: Subtracting values w/list

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dseto200 is offline Offline
56 posts
since Nov 2008
Dec 19th, 2008
0

Re: Subtracting values w/list

Python lists support several methods for example: append, count, extend, index, insert, pop, remove, reverse, sort

http://www.letmegooglethatforyou.com/?q=Python+List
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 20th, 2008
0

Re: Subtracting values w/list

Click to Expand / Collapse  Quote originally posted by dseto200 ...
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.
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help with this code
Next Thread in Python Forum Timeline: Homework Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC