Jump to content

Variable Math Problems? (Oh maths!, how much fun!!) [python]

- - - - -

  • Please log in to reply
3 replies to this topic

#1
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Recently I started learning python- a seemingly harmless language...
So anyway I am writing a program to get the area of a triangle.
This is what I have:

h = raw_input("Enter the height of the triangle: ")

w = raw_input("Enter the width of the triangle: ")

print w

print h

area = w * h

area = area/2

print area

After entering the height and width (and printing them out) it gives me this error:

Traceback (most recent call last):

  File "C:\Python24\TRI Calculator", line 5, in -toplevel-

    area = (w * h)

TypeError: can't multiply sequence by non-int

I'm guessing my input is becoming a string and it doesn't like non strings...
If I set the H/W variables in the code it works fine.

Thanks in advanced and happy programming-
bbqroast
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I believe raw_input would take the raw string no matter what type from the input, the input() function will parse an integer accordingly, so try that.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
as Alexander said, raw_input() always returns string while input() will evalute input. See here and here for a bit more detailed explanation.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
kctan

kctan

    Newbie

  • Members
  • Pip
  • 5 posts
One more point to add, you should cast the input type to float. If not, you will not get decimal value in your answer.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users