You are not logged in.

#1 2009-09-24 15:48:46

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

A little Calculator writed in Python.

Hello.
I write (with some help-Thanks Xyne) a very simple calculator.
It only need python package to work.
Here it is.
It could do base thing's.

 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
number1 = int(raw_input('Write a number: '))
number2 = int(raw_input('Write a number nr.2: '))
znak = raw_input('What to do?')
if znak == '+':
 print number1 + number2
elif znak == '-':
 print number1 - number2
elif znak == '*':
 print number1 * number2
elif znak == '/':
 print number1 / number2
elif znak == '**':
 print number1 ** number2
elif znak == '//':
 print number1 // number2
else:
 print "I don't know what to do with " + znak

Any suggestions or ideas?

Last edited by SpeedVin (2009-09-26 09:41:17)


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#2 2009-09-24 16:02:18

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: A little Calculator writed in Python.

Why not just 'python' in terminal?

$ python
>>> 2+2
4
>>> 2*2
4
>>> 2/2
1
>>> 2**2
4

Sorry if I misunderstood...


This silver ladybug at line 28...

Offline

#3 2009-09-24 17:22:07

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 834
Website

Re: A little Calculator writed in Python.

How about:

    alias calc='bc -l <<<'

$ calc 2+2


You need to install an RTFM interface.

Offline

#4 2009-09-24 17:49:21

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: A little Calculator writed in Python.

anrxc wrote:

How about:

    alias calc='bc -l <<<'

$ calc 2+2

I how to add this alias to my .abshrc or to the code?

lolilolicon wrote:

Why not just 'python' in terminal?

$ python
>>> 2+2
4
>>> 2*2
4
>>> 2/2
1
>>> 2**2
4

Sorry if I misunderstood...

I writed it for newbies that don't know any command of python but have it installed becouse it's dep for another package wink


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#5 2009-09-24 22:22:19

linkmaster03
Member
Registered: 2008-12-27
Posts: 269

Re: A little Calculator writed in Python.

Nice beginner's program there SpeedVin! Keep it up. big_smile Python rocks.

anrxc wrote:

How about:

    alias calc='bc -l <<<'

$ calc 2+2

Or pacman -S calc.

Offline

#6 2009-09-25 16:41:16

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: A little Calculator writed in Python.

linkmaster03 wrote:

Nice beginner's program there SpeedVin! Keep it up. big_smile Python rocks.

anrxc wrote:

How about:

    alias calc='bc -l <<<'

$ calc 2+2

Or pacman -S calc.

Thanks.
I think too that Python Rocks.

Last edited by SpeedVin (2009-09-26 08:44:23)


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#7 2009-09-26 08:45:31

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: A little Calculator writed in Python.

Update
Calculator now can floor division.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
number1 = int(raw_input('Write a number: '))
number2 = int(raw_input('Write a number nr.2: '))
znak = raw_input('What to do?')
if znak == '+':
 print number1 + number2
elif znak == '-':
 print number1 - number2
elif znak == '*':
 print number1 * number2
elif znak == '/':
 print number1 / number2
elif znak == '**':
 print number1 ** number2
elif znak == '//':
 print number1 // number2
else:
 print "I don't know what to do with " + znak

Last edited by SpeedVin (2009-09-26 12:33:51)


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#8 2009-09-26 09:59:07

kgas
Member
From: Qatar
Registered: 2008-11-08
Posts: 718

Re: A little Calculator writed in Python.

speedvin, your code works and it is always good to learn a language and start with such calc program gives the inner of structured programming. I have this simple function in .bashrc and serves the purpose.

function calc() { echo "scale=4;$*" | bc; }

calc 10/3+6 gives 9.0303

Offline

#9 2009-09-26 13:31:47

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: A little Calculator writed in Python.

kgas wrote:

speedvin, your code works and it is always good to learn a language and start with such calc program gives the inner of structured programming. I have this simple function in .bashrc and serves the purpose.

function calc() { echo "scale=4;$*" | bc; }

calc 10/3+6 gives 9.0303

Thanks kgas
I updated calc for a help message (run it with -h).
Anyone can find current code at my github site.


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

Board footer

Powered by FluxBB