[Tutor] More Advanced Calculator.
Kent Johnson
kent37 at tds.net
Mon Feb 21 22:46:04 CET 2005
. Sm0kin'_Bull wrote:
> With your helps i managed to program Advanced Calculator.
> But, I want to change(or add) some to it.
>
> 1. How can i /n/n result bit? I tired.. but, I can't
You can use the print command to print your results. You can give print a list of values and it will
print them separated with spaces. Try
print 'Result:', number1, sign, number2, '=', result
>
> 2. How can i enable it to calculate float-pointing numbers? (Which will
> make it complicated)
Actually, I think you can just replace
number1 = int(raw_input("\nNumber1: "))
with
number1 = float(raw_input("\nNumber1: "))
(etc) to make it work with floating point!
Kent
>
>
> It should looks like this...
>
> Please input data
>
> Number1:
> Mathsmetical Sign:
> Number2:
>
>
> Result:
>
>
> And the source i got
>
> import operator
>
> ops_dict = {'+': operator.add, '*' : operator.mul, '-' : operator.sub,
> '/' : operator.div}
>
> def perform_arithmetic():
> print "Please input data"
> number1 = int(raw_input("\nNumber1: "))
> sign = raw_input("Mathsmetics Sign: ")
> number2 = int(raw_input("Number2: "))
>
> try:
> result = ops_dict[sign](number1, number2)
> except KeyError:
> raise NotImplementedError, "I don't know the sign '%s'" %sign
>
> # add output formatting logic as desired
>
> return result
>
> print perform_arithmetic()
>
> raw_input("")
>
>
> Cheers! :)
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today it's FREE!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list