[Tutor] Advanced Calculator Program...
glingl at aon.at
Sun Feb 20 00:12:13 CET 2005
. Sm0kin'_Bull schrieb:
> I want to make caculator program which enables me to
> enter 2numbers and mathsmatics sign and calculates it.
> I think this is too difficult for newbie like me...
> Please input data
> Mathsmetics Sign:
> (Number1) (Sign) (Number2) = (Result)
> I wrote this but, Error occurs
> print "Please input data"
> number1 = int(raw_input("\nNumber1: "))
> sign = (raw_input("\nMathsmetics Sign: "))
> number2 = int(raw_input("\nNumber2: "))
> total = number1 sign number2
> print number1, sign, number2, '=', total
hey, bull! that's a bit tricky. You had a nice
idea, but it doesn't work in Python. The point is,
that you must not use names for operators in
arithmetic expressions. Those operators have
to be used literally.
*One* way to solve your problem is, e. g., to
use a branching statement for checking, which
operator is used.
So replace the line
total = number1 sign number2
by something similar to
if sign == "+":
total = number1 + number2
elif sign == "-":
total = numer1 - number2
... (for other operators ..)
> please help me
> Cheers! :)
> Express yourself instantly with MSN Messenger! Download today it's FREE!
> Tutor maillist - Tutor at python.org
Telefon: +43 1 713 33 98
Mobil: +43 664 140 35 27
Autor von "Python für Kids"
More information about the Tutor