Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 2nd, 2003, 03:31 AM
markb_1984 markb_1984 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 137 markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 53 m 52 sec
Reputation Power: 9
RE: Problem with method in class

Can someone please advise what I'm doing wrong with the following method. As an introduction, I've got a class containing currency of various denominations, which need to be added up and a total obtained. The method which I am confused on is supposed to convert each denomination of currency to the lowest value of the currency i.e. 'lambs' in my example. The problem is that when I start timesing (*) an attribute in my class, I get a huge number, much longer then the correct result. The code I am confused about is as follows:

def getTotal (self):
self.total = self.lamb * 10 + (self.bleat * 10) + (self.frolic * 50) + (self.ram * 100)
return self.total

In running a test, I commented out everything after self.bleat*10)... and then ran the program typing the values 1 for lamb and 1 for bleat. When the total is printed I expected a result of 11, but instead the result came out as:

11111111111111111111

Any ideas!

Reply With Quote
  #2  
Old April 2nd, 2003, 01:08 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Looks like you are multiplying strings on accident, perhaps. Make sure that when you are doing multiplication that you cast strings to ints or floats.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #3  
Old April 3rd, 2003, 07:29 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
ya I'd check and make sure that your numbers aren't strings, because if they are, it would just print out the string multiple times depending on the number you multiplied it with.

example:

'11' * 10

prints out:

'11111111111111111111'

Reply With Quote
  #4  
Old April 3rd, 2003, 11:09 PM
markb_1984 markb_1984 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 137 markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 53 m 52 sec
Reputation Power: 9
Yeah, that was the problem. I just used the repr() function to convert into integers and that seems to have sorted it.

Thanks.

Reply With Quote
  #5  
Old April 4th, 2003, 04:40 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Quote:
Originally posted by markb_1984
Yeah, that was the problem. I just used the repr() function to convert into integers and that seems to have sorted it.

Thanks.


repr() is fine, but I think maybe int() would be more appropriate as I think it would raise better Exceptions and would make the intentions more clear (repr() may silently convert something to a datatype you don't want to like a list or something whereas int() places more constraints on what the data can be)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > RE: Problem with method in class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT