52ky 发表于 2022-5-3 09:33:20

在 Python 中出现错误“SyntaxWarning: 'str' object is not callable”

问题
我正在学习 Python,我发现了一个错误,在网上搜索了一下,但我仍然不明白为什么。 我做错了什么?

代码:
length = float(input("please input the length: "))
unit = input("please input the unit: ")
if unit == "in" or "inch":
    length_b = float(length / 2.54)
    print("%f inch = %f cm"(length, length_b))
elif unit == "cm":
    length_b = float(length * 2.54)
    print("%f cm = %f inch"(length, length_b))
else:
    print("calculation failed")
我犯了一个错误:
test.py:143: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
print("%f inch = %f cm"(length, length_b))
test.py:146: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
print("%f cm = %f inch"(length, length_b))
回答
您必须在打印的报告中使用 %,如下所示:
length = float(input("please input the length: "))
unit = input("please input the unit: ")
if unit == "in" or unit == "inch":
    length_b = float(length / 2.54)
    print("%f inch = %f cm"%(length, round(length_b,6)))
elif unit == "cm":
    length_b = float(length * 2.54)
    print("%f cm = %f inch"%(length, round(length_b,6)))
else:
    print("calculation failed")
附加:您可以使用 format() 函数轻松尝试:
length = float(input("please input the length: "))
unit = input("please input the unit: ")
if unit == "in" or unit == "inch":
    length_b = float(length / 2.54)
    print("inch is {} and cm is {}".format(length, length_b))
elif unit == "cm":
    length_b = float(length * 2.54)
    print("cm is {} and inch is {}".format(length, length_b))
else:
    print("calculation failed")
输出:
please input the length: 5
please input the unit: in
inch is 5.0 and cm is 1.968504

(problem
I'm learning python. I found an error and searched the Internet, but I still don't understand why. What did I do wrong?
code:
length = float(input("please input the length: "))
unit = input("please input the unit: ")
if unit == "in" or "inch":
length_ b = float(length / 2.54)
print("%f inch = %f cm"(length, length_b))
elif unit == "cm":
length_ b = float(length * 2.54)
print("%f cm = %f inch"(length, length_b))
else:
print("calculation failed")
I made a mistake:
test. py:143: SyntaxWarning: 'str' object is not callable;perhaps you missed a comma?
print("%f inch = %f cm"(length, length_b))
test. py:146: SyntaxWarning: 'str' object is not callable;perhaps you missed a comma?
print("%f cm = %f inch"(length, length_b))
answer
You must use% in the printed report as follows:
length = float(input("please input the length: "))
unit = input("please input the unit: ")
if unit == "in" or unit == "inch":
length_ b = float(length / 2.54)
print("%f inch = %f cm"%(length, round(length_b,6)))
elif unit == "cm":
length_ b = float(length * 2.54)
print("%f cm = %f inch"%(length, round(length_b,6)))
else:
print("calculation failed")
Additional: you can easily try using the format() function:
length = float(input("please input the length: "))
unit = input("please input the unit: ")
if unit == "in" or unit == "inch":
length_ b = float(length / 2.54)
print("inch is {} and cm is {}".format(length, length_b))
elif unit == "cm":
length_ b = float(length * 2.54)
print("cm is {} and inch is {}".format(length, length_b))
else:
print("calculation failed")
Output:
please input the length: 5
please input the unit: in
inch is 5.0 and cm is 1.968504
)



页: [1]
查看完整版本: 在 Python 中出现错误“SyntaxWarning: 'str' object is not callable”