0

I am very new (as in one day) to python and cannot figure it out.

A = raw_input ("Enter A length - ")
B = raw_input ("Enter B length - ")
C = raw_input ("Enter C length - ")
if A*A + B*B > C*C:

As you can see above, I am trying to multiply 'A' by itself and then 'b' by itself and then see if it is less than C multiplied by itself. Probably really simple, but I cannot find it in my Python book or online.

martineau
  • 119,623
  • 25
  • 170
  • 301
user162698
  • 21
  • 1
  • 4

2 Answers2

3

You need to use int to convert the numbers into integers because raw_input returns a string.

A = int(raw_input("Enter A length - "))
B = int(raw_input("Enter B length - "))
C = int(raw_input("Enter C length - "))

if A * A + B * B > C * C:
    # do stuff

What int does is take an object and convert it into an integer. Before, raw_input returned a string. You need to cast to integer with int.

>>> A = raw_input('Test: ')
Test: 3
>>> A
'3'

As you can see, raw_input returns a string. Convert to integer:

>>> int(A)
3

Note: input is not a good idea as it evaluates input taken as literal code. This can raise many errors for wrong inputs—NameError to name one. It can also be dangerous in the sense that malicious code can be executed by the user. Also, for handling wrong inputs, use a try/except. It will raise a ValueError if the object passed is not convertible.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
0
#here you can see we put "int" infront. this is called converting
#since inputs are always strings, we are converting the input into integer
A = int(raw_input ("Enter A length - "))
B = int(raw_input ("Enter B length - "))
C = int(raw_input ("Enter C length - "))

#here we multiply the inputs beforehand
A = A * A
B = B * B
C = C * C

#result variable = A and B combined.
result = A + B

#if we input A = 2 B = 2 C = 5
print(A) #=4
print(B) #=4
print(C) #=25

if (result > C):
    print('A and B are bigger than C')
else: #<---- so this will be called. since 4+4=8 while C=25.
    print('A and B are smaller than C')
Syber
  • 363
  • 2
  • 5
  • 18