IP Project in Python Unit Calculator

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Kendriya Vidyalaya Burdwan

AISSCE Examination-2020
Informatics Practices project
On
Unit Converter
Guided by-Mr. Kundan Kamal
Name : Aman Kumar Mahato
Roll No:

Class : XII- Commerce

Exam : AISSCE

School: Kendriya Vidyalaya Burdwan

Sub Teacher: Mr. Kundan Kamal


This is to certify that AMAN KUMAR MAHATO ,a student of class XII –
Commerce have successfully completed his project for the session
2019-20 on the topic “ANALYSIS OF FINANCIAL MANAGEMENT” under
my guidance. He has taken proper care and shown utmost sincerity in
completing this project.
I certify that this project is up to my expectation and as per the
guidelines issued by CBSE.

Date -

Internal Examiner

External Examiner Mr. Kundan Kamal


PGT CS/IP
We would like to convey our thanks to Mr. Kundan Kamal
(PGT CS/IP) for his immense help and guidance in the completion
of our project. It is only due to his efforts that our project could be
completed successfully.
This report is submitted as a part of our practical examination
included in curriculum of CBSE to be held in the year 2019-20.

AMAN KUMAR MAHATO


Roll No: ……………….
 Introduction
 Objective
 Theoretical Background
 Code
 Conclusion
 Bibliography
This project has been made to simplify the process of conversion of
one unit to another. This project is useful for students and statistical
institutions for getting the results in a simple manner. It is simple and
easy to use. It is time & labour saving too.
The objective of the project is to create a “UNIT CONVERTER“
program that converts quantities expressed in various systems of
measurement to their equivalents in other systems of measurement.
Like many similar programs, it can handle multiplicative scale
changes. It can also handle nonlinear conversions such as Fahrenheit
to Celsius; see Temperature Conversions. The program can also
perform conversions from and to sums of units, such as converting
between meters and feet plus inches.

Basic operation is simple: you enter the units that you want to
convert from and the units that you want to convert to. You can use
the program interactively with prompts, or you can use it from the
command line.
You can change the default behavior of units with various options
given on the command line. See Invoking Units, for a description of
the available options.
Python is a widely used general-purpose, high level programming
language. It was initially designed by Guido van Rossum in 1991 and
developed by Python Software Foundation. It was mainly developed
for emphasis on code readability, and its syntax allows programmers to
express concepts in fewer lines of code.
Python is a programming language that lets you work quickly and
integrate systems more efficiently.

1) Easy to Learn and Use


Python is easy to learn and use. It is developer-friendly and
high level programming language.

2) Expressive Language
Python language is more expressive means that it is more
understandable and readable.

3) Interpreted Language
Python is an interpreted language i.e. interpreter executes the
code line by line at a time. This makes debugging easy and
thus suitable for beginners.

4) Cross-platform Language
Python can run equally on different platforms such as
Windows, Linux, Unix and Macintosh etc. So, we can say that
Python is a portable language.

5) Free and Open Source


Python language is freely available at offical web address.
The source-code is also available. Therefore it is open source.

6) Object-Oriented Language
Python supports object oriented language and concepts of
classes and objects come into existence.

7) Extensible
It implies that other languages such as C/C++ can be used to
compile the code and thus it can be used further in our python
code.

8) Large Standard Library


Python has a large and broad library and prvides rich set of
module and functions for rapid application development.
9) GUI Programming Support
Graphical user interfaces can be developed using Python.

10) Integrated
It can be easily integrated with languages like C, C++, JAVA
etc.

Several books provide conversion factors and algorithms for use in unit
conversion. The available books differ widely in the number of units
covered, the accuracy of the conversion factors, and the algorithms
that some books present for unit conversion. Although one might think
that unit conversion is easy and ``everyone knows how to do it'', the
number of books and the variety of methodologies and algorithms they
present suggest otherwise.
Unit conversion is a multi-step process that involves multiplication or
division by a numerical factor, selection of the correct number of
significant digits, and rounding. This multi-step process is presented
in NIST SP 1038 - 2006 (Section 4.4), including a rounding procedure for
technical documents, specifications, and other applications such
packaged goods in the commercial marketplace and temperature.
Import tkinter as tk

from tkinter import ttk # for the Combobox

root = tk.Tk()

root.title(('UNIT CONVERTER'))

controls = []

cboxindices = []

def onkeypress(event):

# Event handler for entry key pressed

st = event.widget.get()

ch = event.char

deletechar = chr(127) # hex 7F

endflag = 0 # non-zero if key is return

returnchar = '\r' # the return or enter key

ic = event.widget.index(tk.INSERT)

if ord(ch) > 255: return # Ignore arrow keys

if ch == deletechar:

st = st[:ic-1] + st[ic:]

else:

if ch == returnchar: endflag = 1 # signal return key pressed


else:

st = st[:ic] + ch + st[ic:]

process(icoldvalue, st, endflag)

def onselection(event):

# Event handler for Combobox selected

for controlindex in range(len(controls)):

if event.widget == controls[controlindex]: break

unittypeindex = icunittype

if controlindex == unittypeindex:

setupcomboboxes(1)

process(controlindex)

def setupcomboboxes(mode):

# Fill combobox lists

unittypeindex = icunittype

if mode == 0:

# set up unittype list.

units = []

for item in conversiondata:

units.append(item[0])

controls[unittypeindex]['values'] = units

controls[unittypeindex].current(0) # set the current item selection

unittype = controls[unittypeindex].current()

convs = conversiondata[unittype][1].conversions

units = []
for u in convs:

units.append(u[0])

cur = 0

for cindex in (icoldunit, icnewunit):

controls[cindex]['values'] = units

controls[cindex].current(cur)

cur += 1

oldvalue = 1.0

def convert(unittype=0, oldvalue=1.0, oldunit=0, newunit=0):

# convert the current entry value from old to the new units

conv = conversiondata[unittype][1].conversions

newvalue = (float(oldvalue) * conv[oldunit][1] + conv[oldunit][2] \

- conv[newunit][2])/ conv[newunit][1]

newvaluetext = '{:.5}'.format(newvalue)

if newvalue <= 0.001 or newvalue >= 1000.:

newvaluetext = '{:.5e}'.format(newvalue)

controls[icnewvalue].configure(text=newvaluetext)

return newvalue

def entrytype(st='', endflag=0):

noend = '+-Ee'

noreps = '.Ee'

global oldvalue

pm = '+-'

validchars = '0123456789' + noreps + pm # all valid alphanumeric characters


type = 0

stl = len(st)

if stl == 0: type = -1 # a blank entry will fail attempted conversion

if type == 0:

ic = 0

for c in st:

valid = c in validchars # valid = True if character c is present in validchars

if not valid:

type = -1

break

if type == 0 and ic > 0:

norep = c in noreps and c in st[:ic] # True if c is in both Lists

if norep:

type = -1

break

ic = ic + 1

if type == 0:

if st.count('e') + st.count('E')> 1: type = -1 # No repeats of e or E

# test for more then one + or -; note e or E restarts the count

ieE = max (st.find('e'), st.find('E')) + 1

ch = st[-1]

if ch == '+' or ch == '-':

if st[ieE:].count('+') + st[ieE:].count('-') > 1: type = -1

# No '.' allowed in exponent

if ch == '.' and ieE > 1 and st[ieE:].count('.') > 0: type = -1

if type == 0:
if st[-1] not in noend:

# st[-1] is the last element, [-2] the one preceding it, ...

type = 1

else:

# if ch == returnchar: type = -1 # NO! return chr never gets into string.

if endflag != 0: type = -1

if type >= 0: oldvalue = st

return type

def process(controlindex=0, entrystring='', endflag = 0):

global oldvalue

oldvalue = controls[icoldvalue].get()

if controlindex == icoldvalue:

col = 'light blue'

flag = entrytype(entrystring, endflag) # also resets oldvalue

if flag < 0:

col = 'red'

controls[icoldvalue].configure(bg=col)

if flag <= 0:

return

# Get current settings of the controls

unittype =controls[icunittype].current()

oldunit = controls[icoldunit].current()

newunit = controls[icnewunit].current()

# Do the conversion
newvalue = convert(unittype, oldvalue, oldunit, newunit)

class Force: # these subclasses do not need initializers

conversions = (('Newtons', 1.0, 0.), ('pounds-force', 4.448222, 0.),

('dynes', 1.0e-5, 0.), ('kilogram-force', 9.80665, 0.))

class Length:

conversions = (('cms', 1.0, 0.), ('meters', 1.0e2, 0.), ('kilometers', 1.0e5, 0.),

('inches', 2.54, 0.), ('feet', 30.48, 0.), ('yards', 91.44, 0.),

('miles', 1.609344e5, 0.))

class Mass:

conversions = (('grams', 1.0, 0.), ('kilograms', 1.0e3, 0.),

('ounces', 28.34952, 0.), ('pounds', 453.59237, 0.), ('tons', 9.07185e5, 0.))

class Temperature:

conversions = (('Celsius', 1.0, 0.), ('Fahrenheit', 100./180, -160./9),

('Kelvin', 1.0, -273.15))

conversiondata = (('Mass', Mass()), ('Length', Length()),

('Weight', Force()), ('Force', Force()),

('Temperature', Temperature()))

controldata = (('unit type', 'Combobox'), ('old value', 'Entry'),

('old unit', 'Combobox'), (' = ', 'Label'), ('new value', 'Label'),

('new unit', 'Combobox'))


for ic in range(len(controldata)):

if controldata[ic][0] == 'unit type': icunittype = ic

if controldata[ic][0] == 'old value': icoldvalue = ic

if controldata[ic][0] == 'old unit': icoldunit = ic

if controldata[ic][0] == 'new value': icnewvalue = ic

if controldata[ic][0] == 'new unit': icnewunit = ic

for c in controldata:

if c[1] == 'Combobox':

control = ttk.Combobox(root, height = 6, width = 14)

control.bind ('<<ComboboxSelected>>', onselection)

if c[1] == 'Entry':

control = tk.Entry(root, width=8, bd=3, bg='lightgreen')

control.insert (0, '1.0')

control.bind('<Key>', onkeypress)

if c[1] == 'Label':

w = 12

col = 'lightgreen'

t=' '

if c[0] != 'new value':


w=2

col = 'red'

t = '='

control = tk.Label(root, width=w, bd=3, text=t, bg=col)

controls.append(control)

control.pack(side = 'left') # Add the control to the layout

setupcomboboxes(0) # 0 sets up unit type

process()

root.mainloop()
“Program Ends”
Unit conversion is a problem that will not go away, even if the United
States converts to the SI system. Workers in particular fields will
continue to use units such as parsec or micron rather than meter, both
because of tradition and because such units are convenient in size for
the measurements typically used in practice. The compiler algorithms
that we have described are relatively easy to implement, so that units
could be incorporated into a variety of programming languages. These
algorithms make it feasible to implement essentially all known units of
measurement, so that users may use any units they find convenient.
 Informatics Practices(Sumita Arora, DHANPAT RAI &CO)
 www.projectworlds.in
 www.w3resource.com
 www.daniweb.com
 www.codereview.stackexchange.com

You might also like