PDA

View Full Version : Stupid Python Question



Sarai the Geek
April 22nd, 2009, 04:49 AM
Okay, as I said in the title, it is so insanely basic I'm embarrassed that I can't figure it out. I've run umpteenth google searches and have found nothing.

I can't get python to divide. At all. I'm doing some number crunching for a research paper and I need to run the same basic equation over and over again with a single variable, soI figured that writing a quick program could speed things up, but so far I've just ended up pulling a lot of my hair out.

Here's my code:

import math

indiv = raw_input("# of Individuals: ")
dens = indiv/3
print dens

But every time I try to run it it lets me define indiv then spits this out:

File "popecodens.py", line 4, in <module>
dens = spec/3.0
TypeError: unsupported operand type(s) for /: 'str' and 'float'


Gah! What am I missing?

dandaman0061
April 22nd, 2009, 04:58 AM
You need to convert the input into a float or int.



indiv = raw_input("# of Individuals: ")
indiv = int(indiv) # <--- convert the input
dens = indiv/3
print dens


oh and you also don't need to 'import math'... at least for what you've posted here.

Occasionally Correct
April 22nd, 2009, 04:59 AM
raw_input() returns a string. You could try


indiv = int(raw_input("..."))although you may want to ensure that the input is correct. Hope that helps. :)

edit: seconds late! Oh well. ;)

Can+~
April 22nd, 2009, 05:15 AM
Since you're using this for yourself, there's no problem on using "input" instead of "raw_input"

(Also, in python3, input() is the new raw_input())

Sarai the Geek
April 22nd, 2009, 05:38 AM
Sweet, it works now! Thank you so much, guys, also thanks for not making me feel like a total idiot. :)

Sarai the Geek
April 22nd, 2009, 05:52 AM
Oh, one more question. Is there a way to set it to repeat indefinitely, so for example my terminal would like like myeh:

sarai@Sarai-Laptop:~/Desktop$ python popecoreldens.py
Species density: .66
0.0232047112596
Species density: 193
6.78562011075


and on and on until I close the terminal?

sekinto
April 22nd, 2009, 05:57 AM
A while loop.


from time import sleep
while True:
CODE
sleep(SECONDS)

Replace CODE with the code you want to run. Replace SECONDS with the number of seconds you want to pause between each execution.

Sarai the Geek
April 22nd, 2009, 06:01 AM
A while loop.


from time import sleep
while True:
CODE
sleep(SECONDS)Replace CODE with the code you want to run. Replace SECONDS with the number of seconds you want to pause between each execution.

Perfection.
Thanks again!