input - Solving a task in cosmology using Python 2.7.5 -
i looking piece of code takes list of redshifts (i.e. numbers) input user, separated spaces , in 1 go, , automatically assigns them variables a,b,c,d.....m,n,o.
for example, if user gives "0.32 0.53 0.77 0.91 1.1 1.4" input, program should assign 0.32 variable a, 0.53 variable b, 0.77 variable c, , on. not expect user input more 10 numbers.
i have been trying best of knowledge use raw_input function this, no success far.
following planning use simple formula a=3800/(1+a)
find shortest rest-frame wavelength body @ redshift "a" seen emit in optical range, , repeat process other bodies redshifts have been given user. part should present no problem however. real stumbling block described above.
let's got string user using raw_input()
:
astr = raw_input('input: ')
then, there many ways can handle this. easiest might split string list:
alist = astr.split(' ') # returns ['0.32', '0.53', '0.77', '0.91', '1.1', '1.4']
since want them floats instead of string, can use list comprehension:
alist = [float(val) val in astr.split(' ')] # returns [0.32, 0.53, 0.77, 0.91, 1.1, 1.4]
you can put in dictionary:
dic = {j:float(val) j, val in enumerate(astr.split(' '))}
if want name them letters , if sure there less 26, can this:
from string import ascii_lowercase letters dic = {letters[j]:float(val) j, val in enumerate(astr.split(' '))}
finally, use exec
that's bad form , not recommend it.
Comments
Post a Comment