Advertisement
Guest User

Plows shit2

a guest
Jul 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. source2 = "1+2-3*4+3-5/2"
  2. parsed = []
  3. source = list(source2)
  4. parsed2 = []
  5.  
  6. def calculate_string(string):
  7. try:
  8. for subs in string:
  9. if subs.isdigit():
  10. parsed.append(subs)
  11. if subs == '+':
  12. parsed.append('+')
  13. if subs == '-':
  14. parsed.append('-')
  15. if subs == '*':
  16. first = float(string[string.index(subs) - 1])
  17. second = float(string[string.index(subs) + 1])
  18. result = first * second
  19. parsed.append(result)
  20. parsed.remove(string[string.index(subs) - 1])
  21. string.remove(string[string.index(subs) + 1])
  22.  
  23. if subs == '/':
  24. first = float(string[string.index(subs) - 1])
  25. second = float(string[string.index(subs) + 1])
  26. parsed.append(first / second)
  27. parsed.remove(string[string.index(subs) - 1])
  28. string.remove(string[string.index(subs) + 1])
  29. print "First parsed round = "
  30. print parsed
  31.  
  32. for subs2 in parsed:
  33. if subs2 == '+':
  34. first2 = float(parsed[parsed.index(subs2) - 1])
  35. second2 = float(parsed[parsed.index(subs2) + 1])
  36. result2 = first2 + second2
  37. parsed2.append(result2)
  38. parsed.remove(parsed[parsed.index(subs2) + 1])
  39. if subs2 == '-':
  40. first3 = float(parsed[parsed.index(subs2) - 1])
  41. second3 = float(parsed[parsed.index(subs2) + 1])
  42. result3 = first3 - second3
  43. parsed2.append(result3)
  44. parsed.remove(parsed[parsed.index(subs2) + 1])
  45.  
  46. except (ValueError, IndexError):
  47. print "end"
  48.  
  49. return parsed2
  50.  
  51.  
  52. if __name__ == '__main__':
  53. print calculate_string(source)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement