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, 11:23 PM   #11
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
Quote:
Originally Posted by bobfritzelpuff
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.
try adding
from __future__ import division
it makes division give a float if necessary ... otherwise ... if you want a float, one of the members of your division statement must be a float.
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
/* Don't make me use it! */
sykkn is offline   Reply With Quote
Old Aug 27th, 2005, 11:59 PM   #12
bobfritzelpuff
Newbie
 
Join Date: Aug 2005
Posts: 5
Rep Power: 0 bobfritzelpuff is on a distinguished road
Thanks, I got that working. Now how do I generate a random number?

You know what would be helpful? Are there any webpages with a list of python commands and what syntax to use with them? That would be really helpful.

Thanks.
bobfritzelpuff is offline   Reply With Quote
Old Aug 28th, 2005, 12:29 AM   #13
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4 Silvanus is on a distinguished road
The random module has what you're looking for. See http://docs.python.org/lib/module-random.html.

BTW, docs.python.org is where to look for python documentation.
Silvanus is offline   Reply With Quote
Old Aug 28th, 2005, 12:30 AM   #14
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
have you tried www.python.org. They have good documentations and some tutorial for starters.

EDIT: oops, posted at the same time with Silvanos.
OpenLoop is offline   Reply With Quote
Old Aug 28th, 2005, 8:26 AM   #15
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
BTW - you could use the float() function to change the integer into a real number for division:
>>> x = 12
>>> y = 5
>>> x/y
2
>>> x/float(y)
2.3999999999999999
Moldz is offline   Reply With Quote
Old Aug 28th, 2005, 12:07 PM   #16
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
BTW - you could use the float() function to change the integer into a real number for division:
Yeah, that works good and can be used for when you want to keep things as integers but get floating values after division.
Just a small nag - it doesn't "change" the variable. That implies that the float function changes the data type in-place, so after something like
x = 1
float(x)
x will now be a float - 1.0. It does not do that, it just returns a floating point version of the int. So re-worded:
Quote:
BTW - you could use the float() function to get a float variable from the int
Cerulean is offline   Reply With Quote
Old Aug 29th, 2005, 4:00 PM   #17
bobfritzelpuff
Newbie
 
Join Date: Aug 2005
Posts: 5
Rep Power: 0 bobfritzelpuff is on a distinguished road
When I type 'random(1,14)' it tells me "NameError: name 'random' is not defined". What am I doing wrong?
bobfritzelpuff is offline   Reply With Quote
Old Aug 29th, 2005, 4:26 PM   #18
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Did you import the random module?
import random
Once you've done that you use random.random, e.g 'random.random(1, 14)'
Cerulean is offline   Reply With Quote
Old Aug 29th, 2005, 6:49 PM   #19
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
I could have sworn it was " random.randrange(lower, upper) "
Sane is offline   Reply With Quote
Old Aug 29th, 2005, 6:50 PM   #20
bobfritzelpuff
Newbie
 
Join Date: Aug 2005
Posts: 5
Rep Power: 0 bobfritzelpuff is on a distinguished road
Thanks. I got it working now.
bobfritzelpuff 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 6:01 AM.

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