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!

Edited by JordanWalcaraz: n/a

6
Contributors
13
Replies
14
Views
7 Years
Discussion Span
Last Post by hondros
Featured Replies
• 1

I have to agree, there are a lot of things in the 2.x tutorial that I had to seemingly complicate further to make function. I suppose it's for the better of the language though.

• 1

[CODE]#It is a good thing that pyton 3 only has input not raw_input and input as python 2 #Let look a little at it #I do not use python 3,i wait for 3 party module to be portet to python 3 >>> choice = input('Choose your option: ') Choose your …

Input in python 3 return datatype string(same as raw_input in python 2.x)

Change to datatype you need when you calculate.

``````print(add1, "+", add2, "=", int(add1) + int(add2))
print(div1, "/", div2, "=", float(div1) / float(div2))``````

Edited by snippsat: n/a

I don't like that feature of python 3.x
Why did they change it?

Input in python 3 return datatype string(same as raw_input in python 2.x)

Change to datatype you need when you calculate.

``````print(add1, "+", add2, "=", int(add1) + int(add2))
print(div1, "/", div2, "=", float(div1) / float(div2))``````

Thanks snipp, this worked perfectly! Why doesn't 3.x simply read the variable at it's value as entered?

I have to agree, there are a lot of things in the 2.x tutorial that I had to seemingly complicate further to make function. I suppose it's for the better of the language though.

``````#It is a good thing that pyton 3 only has input not raw_input and input as python 2
#Let look a little at it
#I do not use python 3,i wait for 3 party module to be portet to python 3

>>> choice = input('Choose your option: ')
Choose your option: 5
>>> choice
'5'
>>> type(choice)
<class 'str'>
>>> choice + choice
'55'
>>> int(choice) + int(choice)
10

>>> #We can make user input return a interger
>>> choice = int(input('Choose your option: '))
Choose your option: 5
>>> choice + choice
10
>>> type(choice)
<class 'int'>
>>>``````
``````#It is a good thing that pyton 3 only has input not raw_input and input as python 2
#Let look a little at it
#I do not use python 3,i wait for 3 party module to be portet to python 3

>>> choice = input('Choose your option: ')
Choose your option: 5
>>> choice
'5'
>>> type(choice)
<class 'str'>
>>> choice + choice
'55'
>>> int(choice) + int(choice)
10

>>> #We can make user input return a interger
>>> choice = int(input('Choose your option: '))
Choose your option: 5
>>> choice + choice
10
>>> type(choice)
<class 'int'>
>>>``````

Very cool, didn't think of that! Thanks again!
By the way, what is this 3 party module your waiting for?

Edited by JordanWalcaraz: n/a

Some folks are waiting for "3rd party modules" like PIL or the wxPython GUI toolkit to come out for Python3. :)

These modules are not developed by the Python organization but other developers.

I don't like that feature of python 3.x
Why did they change it?

Actually Python2's input() used raw_input() and eval() to come up with a number, a dangerous combination. Now input() returns a string, and it is up to the user to convert it into the correct type.

I have to agree, there are a lot of things in the 2.x tutorial that I had to seemingly complicate further to make function. I suppose it's for the better of the language though.

If you work with Python3 it will be best to get tutorials written for that version:

Swaroop C.H. has rewritten his excellent beginning Python tutorial for Python3:
http://www.swaroopch.com/notes/Python_en:Table_of_Contents

"Dive Into Python" Mark Pilgrim's Free online book, novice to pro, is updated constantly, and has been rewritten for Python3 (check appendix A for Py2-to-Py3 differences)
http://diveintopython3.org/

Also, Python3 comes with a little utility program called 2to3.py that converts Python2 code to Python3 code.

Actually Python2's input() used raw_input() and eval() to come up with a number, a dangerous combination. Now input() returns a string, and it is up to the user to convert it into the correct type.

I didn't like it because it was different. I never really looked at Pythons source code. Guess I should sometime, just to see how things work.

If you work with Python3 it will be best to get tutorials written for that version:

Swaroop C.H. has rewritten his excellent beginning Python tutorial for Python3:
http://www.swaroopch.com/notes/Python_en:Table_of_Contents

"Dive Into Python" Mark Pilgrim's Free online book, novice to pro, is updated constantly, and has been rewritten for Python3 (check appendix A for Py2-to-Py3 differences)
http://diveintopython3.org/

Also, Python3 comes with a little utility program called 2to3.py that converts Python2 code to Python3 code.

These are great! Thanks a lot vega, glad to have even more resources to learn from :)

Actually Python2's input() used raw_input() and eval() to come up with a number, a dangerous combination. Now input() returns a string, and it is up to the user to convert it into the correct type.

And BDFL didn't like it, so that's another reason why its changed :).

Hey, just to show you something dangerous you can do with input(), (If the module 'sys' has been imported):

``````>>> input("> ")
> sys.stdout.write('OGGA')
OGGA``````

I know it's not much, but think of things you could do with that

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.