![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Posts: 5
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
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 ![]() |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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; |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
OpenLoop, once you've defined a variable, it keeps its type for its lifespan. You can't change it.
|
|
|
|
|
|
#5 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
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 >>> |
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I could swear...
Ah well... |
|
|
|
|
|
#7 |
|
Professional Programmer
|
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. |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Modulo is "%" in Python too.
|
|
|
|
|
|
#9 |
|
Professional Programmer
|
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;} |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Well, you're right and wrong IIRC. Python types are kept for the variable lifespan. For example, when you do
x = 3 x = "foo" So really, the type is kept for it's entire lifespan. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|