Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. def main():
  5.     #userinput = input("Enter equation")
  6.     global userinput
  7.     userinput = 'x^2-3x+4'
  8.     getValues(userinput)
  9.  
  10.  
  11. def getValues(userinput):
  12.     try:
  13.         x2 = list(re.findall(r'([-+]*)\s?(\d*)\s?(\w*)\s?\^\s?(\d*)',userinput)[0])
  14.         x = list(re.findall(r'(?<!\^)([+-]*)\s?(\d*)\s?(\w*)\s?[-+]',userinput)[0])
  15.     except IndexError:
  16.         print('Check your equation. You messed up bigtime.')
  17.     try:
  18.         const = list(re.findall(r'([-+]*)(?<![\w])\s?(\d*)(?![-+\w])',userinput)[0])
  19.     except IndexError:
  20.         const = ['+','0']
  21.  
  22.     if x2[0] == '':
  23.         x2[0] = '+'
  24.     if x[0] == '':
  25.         x[0] = '+'
  26.     if const[0] == '':
  27.         const[0] = '+'
  28.  
  29.     if x2[1] == '':
  30.         x2[1] = '1'
  31.     if x[1] == '':
  32.         x[1] = '1'
  33.     if const[1] == '':
  34.         const[1] = '0'
  35.  
  36.     print("""x^2:
  37.    Sign:{}
  38.    Coefficient:{}
  39.    Variable:{}
  40.    Index:{}""".format(x2[0],x2[1],x2[2],x2[3]))
  41.     print()
  42.     print("""x:
  43.    Sign:{}
  44.    Coefficient:{}
  45.    Variable:{}""".format(x[0],x[1],x[2]))
  46.     print()
  47.     print("""Constant:
  48.    Sign:{}
  49.    Constant:{}""".format(const[0],const[1]))
  50.    
  51.  
  52. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement