Python2_Amateur
Python2_Amateur

Reputation: 21

Why am I being displayed "TypeError: a float is required"?

My main issue is this error being displayed:

TypeError: a float is required

I haven't tried much as I don't really know what I'm doing, being very new to coding and all, so I would appreciate some patient advice on the matter.

from math import sqrt

n = raw_input('Type number here: ')

def square_root(n):
  """Returns the square root of a number."""
  square_rooted = sqrt(n)
  print "%d square rooted is %d." % (n, square_rooted)
  return square_rooted

square_root(n)

I want to be able to type in a number and it display the square root of it.

Upvotes: 2

Views: 124

Answers (4)

Jon
Jon

Reputation: 23

This worked for me, had to fix syntax based on my version of python-

from math import sqrt

n = input('Type number here: ')

n = float(n)

def square_root(n):

   #"""Returns the square root of a number."""

   square_rooted = sqrt(n)

   print("%d square rooted is %d." % (n, square_rooted))
   return square_rooted

square_root(n)

Upvotes: 0

As mentioned above perhaps python3 is the better python version to be using if you are new but the python 2 solution would look like below. Where we use %f to indicate our number is a float. In addition on line 2 we wrap the raw_input() statement in a float() function. This allows the python interpreter to understand we expect a float value.

from math import sqrt

n =float(raw_input('Type number here: '))

def square_root(n):
  """Returns the square root of a number."""
  square_rooted = sqrt(n)
  print "%f square rooted is %f." % (n, square_rooted)
  return square_rooted

square_root(n)

The python 3 version would be below just a few minor edits. The input line would now become input() not raw_input()... Also the print statement would use parentheses on the sides:

from math import sqrt

n =float(input('Type number here: '))

def square_root(n):
  """Returns the square root of a number."""
  square_rooted = sqrt(n)
  print("%f square rooted is %f." % (n, square_rooted))
  return square_rooted

square_root(n)

Upvotes: 0

jose_bacoy
jose_bacoy

Reputation: 12684

Change your codes to convert string to float. Input results into a string format.

square_rooted = sqrt(float(n))

Also; change your code in displaying the values. Use %s instead of number (%d)

"%s square rooted is %s."

Sample:

Type number here: 81
81 square rooted is 9.0.

Upvotes: 2

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

Some issues/fixes for your code

  • You need to cast the string you got from raw_input

  • To display float, use %f string formatting

So the code will change to

from math import sqrt
#Convert string obtained from raw_input to float
n = float(raw_input('Type number here: '))
def square_root(n):
  """Returns the square root of a number."""
  square_rooted = sqrt(n)
  print "%f square rooted is %f." % (n, square_rooted)
  return square_rooted

square_root(n)

And the output will look like

Type number here: 4.5
4.500000 square rooted is 2.121320.

Upvotes: 2

Related Questions