Advertisement
waelwindows92

Pytana

May 5th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. import datetime
  2.  
  3. #Useful Functions
  4. def clamp(n, minn, maxn):
  5.     return max(min(maxn, n), minn)
  6. def ncomma(x):
  7.     return "{:,}".format(x)
  8.  
  9. def getints(q,op):
  10.     add = 0
  11.     first = 0
  12.     second = 0
  13.     offset = 0
  14.     err0 = "Mathematical Error"
  15.     for i in q:
  16.         if i != op:
  17.             add += 1
  18.         else:
  19.             break
  20.     #print(q[add:])
  21.     #print(q[add:add + 2])
  22.     if q[add:add + 2] == op + " ":
  23.         offset = add + 2
  24.     elif q[add:add + 1] == op:
  25.         offset = add + 1
  26.     else:
  27.         print(err0)
  28.         return 0
  29.     first = int(q[:add])
  30.     second = int(q[offset:])
  31.     #Validation
  32.     if first - (first - 1) != 1:
  33.         return 0
  34.     if second - (second - 1) != 1:
  35.         return 0
  36.     return first,second
  37.    
  38. # Actual Programs
  39. def getdateformated():
  40.     intime = datetime.datetime.now()
  41.     ampm = ""
  42.     hour = 0
  43.     time = ""
  44.     gmt = input ("Please write your local greenwich mean time code.")
  45.     monthlist = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
  46.     month = monthlist[intime.month - 1]
  47.     dayappend = ["st","nd","rd","th"]
  48.     dayint = intime.day
  49.     day = str(dayint) + dayappend[clamp(dayint,1,4) - 1]
  50.     dayname = intime.strftime("%A")
  51.     if gmt[:1] == "+":
  52.         #print("it's " + str(intime.hour + int(gmt[1:])) + ":" + str(intime.minute))
  53.         hour = intime.hour + int(gmt[1:])
  54.         #print("add")
  55.     elif gmt[:1] == "-":
  56.         #print("it's " + str(intime.hour - int(gmt[1:])) + ":" + str(intime.minute))
  57.         hour = intime.hour - int(gmt[1:])
  58.         #print("sub")
  59.     elif gmt[:1] == "0":
  60.         #print("it's " + str(intime.hour) + ":" + str(intime.minute))
  61.         hour = intime.hour
  62.     else:
  63.         print("Error: Wrong GMT code written. Please write it again.")
  64.         return False
  65.     if hour > 12:
  66.         ampm = " PM"
  67.         hour -= 12
  68.     elif hour < 12:
  69.         ampm = " AM"
  70.     time = str(hour) + ":" + str(intime.minute) + ampm
  71.     print("it's " + dayname + " the " + day + " of " + month + ", " + str(intime.year) + ". " + time)
  72. #Mathematic Function
  73. def domath(q):
  74.     first = 0
  75.     second = 0
  76.     result = 0
  77.     op = ""
  78.     if "+" in q:
  79.         op = "+"
  80.     elif "-" in q:
  81.         op = "-"
  82.     elif "*" in q:
  83.         op = "*"
  84.     elif "/" in q:
  85.         op = "/"
  86.     if getints(q,op) == 0:
  87.         return
  88.     elif getints(q,op) != 0:
  89.         first,second = getints(q,op)
  90.     if op == "+":
  91.         result = first + second
  92.     elif op == "-":
  93.         result = first - second
  94.     elif op == "*":
  95.         result = first * second
  96.     elif op == "/":
  97.         result = first / second
  98.        
  99.     print(q + " is " + ncomma(result))
  100.  
  101. def core():
  102.     q = input("Please ask me a question.")
  103.     if "date" in q or "time" in q:
  104.         getdateformated()
  105.     elif "+" in q or "-" in q or "*" in q or "/" in q:
  106.         domath(q)
  107. core()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement