Member Avatar for sravan953
# A simple calculator

x=input("What do you want to do? \n 1) Add \n 2) Subtract \n 3) Multiply \n 4) Divide \n")

#This is a comment, note that single line comments start with a hash '#'
"""'x' is a variable. The 'input' statement accepts a number where as the 'raw_input' accepts a String. Note that multiline
comments start with a three quotes"""

if x==1:
    a=input("Enter a number: ")
    b=input("Enter another number: ")
    c=a+b
    print ("The sum of "+a+" and "+b+" is: "+(a+b))
elif x==2:
    a=input("Enter a number: ")
    b=input("Enter another number: ")
    c=a-b
    print ("The difference of "+a+" and "+b+" is: "+(a-b))
elif x==3:
    a=input("Enter a number: ")
    b=input("Enter another number: ")
    c=a*b
    print ("The product of "+a+" and "+b+" is: "+(a*b))
elif x==4:
    a=input("Enter a number: ")
    b=input("Enter another number: ")
    c=a/b
    print ("The quotient of "+a+" and "+b+" is: "+(a/b))
    
""" In Python, contrary to Java, you don't have to explicitly specify the data type of a variable, instead Python
implicitly determines the data type! Note that commenst spawning over many lines beging with three quotes """

raw_input("\nPress any character to quit the program.")

# Since we don't want the program to quit immediately after printing the output, we add a 'raw_input' statement
# Try removing the 'raw_input' statement at the last and see what happens.

The code I used above just doesn't work! It says it cannot concatenate String and Int objects! Is there a way to solve this?

Thanks

you can convert the ints to strings with str(), try:

print ("The sum of "+str(a)+" and "+str(b)+" is: "+str(a+b))

Enjoy!

Try to change your print line to:
print ("The sum of "+str(a)+" and "+str(b)+" is: "+str(a+b))
or even better:
print ("The sum of %s and %s is: %s " % (a, b, (a+b)))

Now remember in Python2 input() returns a number and Python3 input() returns a string. In either case you have to do some changes.

Let us all hope that this code is not from a Python book.

Very useful information.
Thanks to all my frnds

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.