相关疑难解决方法(0)

如何用Python舍入到2位小数?

我在这段代码的输出中得到了很多小数(华氏温度到摄氏温度转换器).

我的代码目前看起来像这样:

def main():
    printC(formeln(typeHere()))

def typeHere():
    global Fahrenheit
    try:
        Fahrenheit = int(raw_input("Hi! Enter Fahrenheit value, and get it in Celsius!\n"))
    except ValueError:
        print "\nYour insertion was not a digit!"
        print "We've put your Fahrenheit value to 50!"
        Fahrenheit = 50
    return Fahrenheit

def formeln(c):
    Celsius = (Fahrenheit - 32.00) * 5.00/9.00
    return Celsius

def printC(answer):
    answer = str(answer)
    print "\nYour Celsius value is " + answer + " C.\n"



main()
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何使程序围绕小数点后两位的每个答案?

python rounding

179
推荐指数
14
解决办法
50万
查看次数

ValueError:数学域错误

我只是用Python测试工程中的数值方法的一个例子.

from numpy import zeros, array
from math import sin, log
from newtonRaphson2 import *

def f(x):
    f = zeros(len(x))
    f[0] = sin(x[0]) + x[1]**2 + log(x[2]) - 7.0
    f[1] = 3.0*x[0] + 2.0**x[1] - x[2]**3 + 1.0
    f[2] = x[0] + x[1] + x[2] -5.0
    return f

x = array([1.0, 1.0, 1.0])
print newtonRaphson2(f,x)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它显示以下错误:

File "example NR2method.py", line 8, in f
    f[0] = sin(x[0]) + x[1]**2 + log(x[2]) - 7.0
ValueError: math domain error …
Run Code Online (Sandbox Code Playgroud)

python runtime-error logarithm

89
推荐指数
1
解决办法
24万
查看次数

为什么我会得到ValueError:math域错误?

我写了一个名为的函数analyze_the_shape,它接受一个2D顶点列表,使得列表按照2D欧几里德空间中顶点的顺时针遍历顺序排列.

我在解释器中调用它并给出[(0, 0), (0, 4.0), (4.0, 4.0), (4.0, 0)]输入但是我得到了ValueError : math domain error.我希望看到return ["SQUARE", 4.0].我能做什么 ?

import math

def analyze_the_shape(liste):
    if len(liste) == 2 :
        d = ( (liste[1][0] - liste[0][0])**2 + (liste[1][1] - liste[0][1])**2 )**(0.5)   
        return ["LINESEGMENT", d ] 
    if len(liste) == 4 :
        d1 = abs(( (liste[1][0] - liste[0][0])**2 + (liste[1][1] - liste[0][1])**2 )**(0.5))
        d2 = abs(( (liste[2][0] - liste[1][0])**2 + (liste[2][1] - liste[1][1])**2 )**(0.5))
        d3 = abs(( (liste[3][0] - …
Run Code Online (Sandbox Code Playgroud)

python

6
推荐指数
2
解决办法
4万
查看次数

python数学域错误-sqrt

是什么原因引起的问题?

from math import sqrt
print "a : "
a = float(raw_input())
print "b : "
b = float(raw_input())
print "c : "
c = float(raw_input())
d = (a + b + c)/2
s = sqrt(d*(d-a)*(d-b)*(d-c))
print "a+b+c =", a, b, c
print "Distr. =", d*2, "Area =", s
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
   File "C:/Python27/fájlok/háromszög terület2.py", line 11, in <module>
       s = sqrt(d*(d-a)*(d-b)*(d-c))
ValueError: math domain error
Run Code Online (Sandbox Code Playgroud)

python math dns sqrt

4
推荐指数
2
解决办法
3万
查看次数

标签 统计

python ×4

dns ×1

logarithm ×1

math ×1

rounding ×1

runtime-error ×1

sqrt ×1