fork(8) download
  1. def convert_to_float(frac_str):
  2. try:
  3. return float(frac_str)
  4. except ValueError:
  5. num, denom = frac_str.split('/')
  6. try:
  7. leading, num = num.split(' ')
  8. whole = float(leading)
  9. except ValueError:
  10. whole = 0
  11. frac = float(num) / float(denom)
  12. return whole - frac if whole < 0 else whole + frac
  13.  
  14.  
  15. print convert_to_float('3')
  16. print convert_to_float('3/2')
  17. print convert_to_float('1 1/2')
  18. print convert_to_float('-1 1/2')
Success #stdin #stdout 0s 7736KB
stdin
Standard input is empty
stdout
3.0
1.5
1.5
-1.5