0

I've been learning python on codeacademy and I wanted to make a program that could help me with my homework and I started with pythagoras a2+b2=c2 and it works perfectly on codeacademy but when I try it on the real python program it won't work and it closes before I can read what's wrong.

a = input ("what is a")
b = input ("what is b")

a = a*a
b = b*b
c =  a+b

from math import sqrt
c = sqrt (c)

print (c)

i know its pretty basic but im still learning also im not sure what version of python code academy is but im pretty sure the python program im useing is 3

1
  • I think this is more readable (and definitely much sorter): c = sqrt(a**2 + b**2) Commented Mar 1, 2013 at 19:47

5 Answers 5

3

I believe you have a type conververtion problem here. So you need to convert it to an integer:

from math import sqrt
a = int(raw_input("what is a: "))
b = int(raw_input("what is b: "))

a = a*a
b = b*b
c = a+b

c = sqrt (c)
print (c)

Aslo, to not have to program close before you get to read the output, you would need to run the pythonfile from the terminal.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you all for posting you all helped me with it and it does work now thanks!!
yeh i know than but i also fixed that with adding another input saying end
0

input returns a string (type str). in order for multiplication to work, you must create an integer (type int) from them like this:

a = int(input("what is a?"))
b = int(input("what is b?"))

or if you want the user to be able to type in decimals, use float:

a = float(input("what is a?"))
b = float(input("what is b?"))

Comments

0

Python 2:

>>> a=input()
123
>>> a #is an int
123

Python 3:

>>> a=input()
123
>>> a #is a string
'123'

Comments

0

You've got a type conversion issue. Cast to an float (or a int, here's a little about the difference between the two) and it will all be fine.

a = float(input ("what is a"))
b = float(input ("what is b"))

You should also look into using your Python interpreter. This is what I got when I tried to step through the code manually:

>>> a = input('what is a')
what is a3
>>> a*a # I put 3 in as my number, but it gave me the str value of '3'!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'

I also recommend try...except to help give you better error messages. If you use while it will also make it so that you can make sure you have something to work with eg:

# Make them keep inputting "a" until they give you something 
# you can actually work with!
while 1:
    try:
        a = float(input ("what is a"))
        break
    except TypeError:
        print('That was not a number! please try again')

Note: this is not something which would happen in Python 2.x, as input can return an int.

Comments

-1

If you run this in the python debugger you'll see what's happening line by line and be able to tell what line is the problem. I know you are green in Python but learning to use a debugger as soon as possible will make your learning process go so much faster. Try:

python -m pdb myscript.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.