Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “为什么我会被展示”;TypeError:需要浮动“;?_Python - Fatal编程技术网

Python “为什么我会被展示”;TypeError:需要浮动“;?

Python “为什么我会被展示”;TypeError:需要浮动“;?,python,Python,我的主要问题是显示此错误: TypeError: a float is required 我没有尝试太多,因为我不知道我在做什么,因为我对编码和所有方面都很陌生,所以我希望能在这方面得到一些耐心的建议 from math import sqrt n = raw_input('Type number here: ') def square_root(n): """Returns the square root of a number.""" square_rooted = sqrt(

我的主要问题是显示此错误:

TypeError: a float is required
我没有尝试太多,因为我不知道我在做什么,因为我对编码和所有方面都很陌生,所以我希望能在这方面得到一些耐心的建议

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)

我希望能够键入一个数字,并显示其平方根。

代码的一些问题/修复

  • 你需要把你从他那里得到的绳子扔出去

  • 要显示浮动,请使用
    %f
    字符串格式

因此,代码将更改为

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)
输出结果如下所示

Type number here: 4.5
4.500000 square rooted is 2.121320.

更改代码以将字符串转换为浮点。将结果输入为字符串格式

square_rooted = sqrt(float(n))
还有,;在显示值时更改代码。使用%s而不是数字(%d)

样本:

Type number here: 81
81 square rooted is 9.0.

如上所述,如果您是新手,也许python3是更好的python版本,但是Python2解决方案如下所示。其中我们使用%f表示我们的数字是一个浮点数。此外,在第2行中,我们将原始的_input()语句包装在float()函数中。这使python解释器能够理解我们期望的浮点值

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)
Python3版本将在下面进行一些小的编辑。输入行现在将变成input()而不是raw\u input()。。。print语句也会在两侧使用括号:

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)

这对我来说很有效,必须根据我的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)

如果您是新的编码,从Python 3开始;Python2已经过时了。非常好的建议。你以后会感谢他的。
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)