Hack Forums
[Python] A tool with calculator and binary to text and more!! - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] A tool with calculator and binary to text and more!! (/showthread.php?tid=4294730)



A tool with calculator and binary to text and more!! - SIM-2044150 - Jun 19, 2014

Well this is something I made in Python it has a couple of cool things :)

if code below doesnt work then use this pastebin link

http://pastebin.com/iSPCuWg0

PHP Code

<?php 
#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import math
import subprocess 
as sp
import json
import urllib
import platform
import sys
import smtplib

os 
= platform.system()
pcname = platform.node()

def main():
    
cls()
    print(
"1) Calculator")
    print(
"2) IP Tools")
    print(
"3) Binary")
    
sin = input('>>> ')
    if 
sin == 1:
      
calc()
    if 
sin == 2:
      
ipt()
    if 
sin == 3:
      
bint()

def calc():
    
def add():
      
cls()
      print(
"Type Number 1")
      
n1 = input(">>> ") 
      print(
"Type Number 2")
      
n2 = input(">>> ")
      
tot = n1+n2
      
print(str(n1)+" + "+str(n2)+" = "+str(tot))
    
def sub():
      
cls()
      print(
"Type Number 1")
      
n1 = input(">>> ") 
      print(
"Type Number 2")
      
n2 = input(">>> ")
      
tot = n1-n2
      
print(str(n1)+" - "+str(n2)+" = "+str(tot))      
    
def mul():
      
cls()
      print(
"Type Number 1")
      
n1 = input(">>> ") 
      print(
"Type Number 2")
      
n2 = input(">>> ")
      
tot = n1*n2
      
print(str(n1)+" x "+str(n2)+" = "+str(tot))
    
def div():
      
cls()
      print(
"Type Number 1")
      
n1 = input(">>> ") 
      print(
"Type Number 2")
      
n2 = input(">>> ")
      
tot = n1/n2
      r 
= n1%n2
      
print(str(n1)+" / "+str(n2)+" = "+str(tot)+" R"+str(r))
    
cls()
    print 
'1) Addition'
    
print '2) Subtraction'
    
print '3) Multiplacation'
    
print '4) Division'
    
cin = input('>>> ')
    if 
cin == 1:
      
add()
    if 
cin == 2:
      
sub()
    if 
cin == 3:
      
mul()
    if 
cin == 4:
      
div()

def ipt():
    
cls()
    print 
"1) Whats my IP"
    
print "2) IP to Location"
    
ipti = input('>>> ')

    if 
ipti == 1:
      
cls()
      
ip = urllib.urlopen('http://www.networksecuritytoolkit.org/nst/tools/ip.php').read()
      print(
'Your IP is '+ip)
      
rq()

    if 
ipti == 2:
      
cls()
      print(
"1) My Location")
      print(
"2) IP to Location")

      
uin = input(">>> ")

      if 
uin == 1:
      
cls()
      
ip = urllib.urlopen("http://www.networksecuritytoolkit.org/nst/tools/ip.php").read()
      
jipd = "http://ipinfo.io/"+ip+"/geo"
      
jd = json.load(urllib.urlopen(jipd))
      print(
"You live in "+jd["city"]+", "+jd["region"]+" "+jd["country"])
      
rq()
      if 
uin == 2:
      
cls()
      print(
"Type IP to get Location of ")
      
ip = raw_input(">>> ")
      
jipd = "http://ipinfo.io/"+ip+"/geo"
      
jd = json.load(urllib.urlopen(jipd))
      print(
"You live in "+jd["city"]+", "+jd["region"]+" "+jd["country"])
      
rq()

def bint():
    
cls()
    print(
"1) Binary to text")
    print(
"2) Text to binary")
    
uin = input(">>> ")      

    if 
uin == 1:
      
cls()
      print(
"Binary to convert")
      
binaryt = input(">>> ")
      print(
''.join(chr(int(binaryt[i:i+8], 2)) for i in xrange(0, len(bin_text), 8)))
      
rq()
    if 
uin == 2:
      
cls()
      print(
"Text to convert")
      
raw = raw_input(">>> ")
      print(
''.join('{:08b}'.format(ord(c)) for c in raw))
      
rq()

def rq():
    
cls()
    print 
'Do you want to restart?(y/n)'
    
gbq = raw_input('>>>')
    
a = gbq.lower
    cls
()
    if 
a == 'y' or 'yes':
      
start()
    if 
a == 'n' or 'no':
      
import sys
      
print 'Exiting'
      
sys.exit(0)
      exit()

def cls():
    if 
os == 'Windows' or 'Vista' or 'XP':
      
sp.call('cls', shell=True)
    
elif os == 'Linux' or 'Linux2' or 'Darwin':
      
sp.call('clear', shell=True)
    else:
      print 
'\nUnable to clear screen!'

main() 



* - glob - Jun 19, 2014

HF code tags break formatting so you should keep that in mind and use [ php ] tags instead.


* - HbananF - Jun 19, 2014

Looks good, thanks for sharing. I'm going to give a shot at learning Python again. :)


* - SIM-2044150 - Jun 19, 2014

(Jun 19, 2014 04:13 PM)glob Wrote:  Formatting seems to still be broken. Some of your if statements are out of place in the calculator section

Well idk the code on my desktop works ill put it on PasteBin

http://pastebin.com/iSPCuWg0


* - ßlack мesa - Jun 20, 2014

Looks all well a good, but there are many areas where your code could be improved. For example, each function inside your calculator function uses the following lines of code.
PHP Code

<?php 
cls
()
print(
"Type Number 1")
n1 = input(">>> ")
print(
"Type Number 2")
n2 = input(">>> ") 

You will learn a lot if you looked into Object-Oriented Programming and started creating classes in Python. This would allow you to create a Calculator class that has a single inputting method, which can then easily be used for all the rest of your calculator's operations.

Good luck!