Hi everyone,
I know this is a very beginner inquiry, but I can't seem to figure out exactly what it is that I'm doing wrong.
I'm using this tutorial: http://www.sthurlow.com/python/lesson05/ to build a simple calculator program.
My version of Python is 3.1.2
here is my code:
loop = 1
choice = 0
while loop == 1:
print("Welcome to calculator.py")
print("your options are:")
print(" ")
print("1] Addition")
print("2] Subtraction")
print("3] Multiplication")
print("4] Division")
print("5] Quit Calculator.py")
print(" ")
choice = input('Choose your option: ')
if choice == "1":
add1 = input("Add this: ")
add2 = input("to this: ")
print(add1, "+", add2, "=", add1+add2)
elif choice == "2":
sub2 = input('Subtract this: ')
sub1 = input('From this: ')
print(sub1, "-", sub2, "=", sub1-sub2)
elif choice == "3":
mul1 = input('Multiply this: ')
mul2 = input('with this: ')
print(mul1, "*", mul2, "=", mul1*mul2)
elif choice == "4":
div1 = input('Divide this: ')
div2 = input('by this: ')
print(div1, "/", div2, "=", div1/div2)
elif choice == "5":
loop = 0
When I try to perform addition, instead of adding the two variables together, it simply places them next to each other. For example: 2 + 2 = 22
When I try to subtract, I get this message:
Traceback (most recent call last):
File "/Users/jordanwillis/Documents/calculator.py", line 32, in <module>
print(sub1, "-", sub2, "=", sub1-sub2)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
When I try to multiply, I get this:
Traceback (most recent call last):
File "/Users/jordanwillis/Documents/calculator.py", line 36, in <module>
print(mul1, "*", mul2, "=", mul1*mul2)
TypeError: can't multiply sequence by non-int of type 'str'
Division:
Traceback (most recent call last):
File "/Users/jordanwillis/Documents/calculator.py", line 40, in <module>
print(div1, "/", div2, "=", div1/div2)
TypeError: unsupported operand type(s) for /: 'str' and 'str'
I was hoping I had changed most of what I needed to from the tutorial to work in 3.1, although I must be missing something or several things. I realize the answer might be incredibly simple but I just started learning programming today :)
If anyone could take a minute to help me out, I'd appreciate it.
Thanks!