1

I've just started learning Python. As this is my first language, don't be harsh on me if this is too easy. I can't figure out how to solve this. This is what i've programed:

a=input("Enter pyramid base size ")
H=input ("Enter pyramid height size ")
P=a*a+2*a*h
print (P)

But this comes out:

Enter pyramid base size 2
Enter pyramid height size 2
Traceback (most recent call last):
  File "D:\Program Files (x86)\Python\pyramid.py", line 3, in <module>
    P=a*a+2*a*h
TypeError: can't multiply sequence by non-int of type 'str'
2
  • Python is case sensitive so you need H instead of h: P=aa+2*aH Commented May 8, 2013 at 20:49
  • What are you trying to get for output? Commented May 8, 2013 at 20:50

2 Answers 2

3

The type of the input received is a string but for your equation, numbers are needed. You can convert your input to an integer or float and then do the math

a = int(a)
h = float(h)

Now you might need to check if the input can be converted into a number and you can do something like:

try:
  h = int(h)
except ValueError:
  print 'the input couldnt be converted into an integer'

Edit

So, here is what your code should look like

a = input("Enter pyramid base size ")
h = input ("Enter pyramid height size ")

try:
  a = int(a)
  h = int(a)
  p = a*a + 2*a*h

  print (p)
except: ValueError:
  print 'Input cannot be converted to an integer'
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry if bothering you, but as I am new and started learning Python today, this is advanced for me, and I still can't figure it out
1

Your inputs a and b are strings. While you can multiply a string by a number to repeat the string (for example, 'a' * 5 == 'aaaaa'), you cannot multiply a string by a string.

If you want P to be a number, wrap your input calls in int(input(...)).

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.