Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 27th, 2005, 12:10 PM   #1
bobfritzelpuff
Newbie
 
Join Date: Aug 2005
Posts: 5
Rep Power: 0 bobfritzelpuff is on a distinguished road
Integer Problems

I was just teaching myself python today, and I was writing a little program which required me to divide 12 by 5. The answer it returned was 2. I need the answer to be correct (2.4) in order for the program to work, yet I can not find out how to stop python giving me the floor of my answer. Is it supposed to do this, and how do I stop it?

Thanks.
bobfritzelpuff is offline   Reply With Quote
Old Aug 27th, 2005, 12:27 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
I've never used python, but integers can only contain - the name says it - integers (whole numbers). Use a double or a float to get 2.4 as your answer
Polyphemus_ is offline   Reply With Quote
Old Aug 27th, 2005, 1:06 PM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Actually Polyphemus, in python you can store whatever you want in a variable.
x = 0
x = "HAHA"
x =  1.5

Bob, I'm no python expert so correct me if i'm wrong. Try to store the numbers as floats:
x = 12.00
y = 5.00
print x/y;
OpenLoop is offline   Reply With Quote
Old Aug 27th, 2005, 1:17 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
OpenLoop, once you've defined a variable, it keeps its type for its lifespan. You can't change it.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 27th, 2005, 1:49 PM   #5
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by Ooble
OpenLoop, once you've defined a variable, it keeps its type for its lifespan. You can't change it.
We're talking Python, right?

source:
x = 12.00
y = 5.00
print x, r" / ", y, " = ", x/y;

x = 1
print x
x = "See now x is a string instead of integer, next it'll be a float"
print x
x =  1.5
print x

output:
>>> 
12.0  /  5.0  =  2.4
1
See now x is a string instead of integer, next it'll be a float
1.5
>>>
OpenLoop is offline   Reply With Quote
Old Aug 27th, 2005, 2:14 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I could swear...

Ah well...
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 27th, 2005, 5:13 PM   #7
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
woooooops
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}

Last edited by iignotus; Aug 27th, 2005 at 5:31 PM.
iignotus is offline   Reply With Quote
Old Aug 27th, 2005, 5:26 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Modulo is "%" in Python too.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 27th, 2005, 5:32 PM   #9
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
Yea I'm stupid I've had a bad day lol.
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Aug 27th, 2005, 7:52 PM   #10
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Well, you're right and wrong IIRC. Python types are kept for the variable lifespan. For example, when you do
x = 3
x = "foo"
First an int object is created and will be pointed to by the name "x". With the second line, you remove the reference to the 'thing' that the name "x" was pointing to - in this case, the int object. If there is nothing else referencing it (and nothing else will as it is an int) then the variable will be deleted by the garbage collector. Then a new string object will be constructed with a value of "foo", and it will be pointed to by the name "x".
So really, the type is kept for it's entire lifespan.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC