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

代码:
  1. length = float(input("please input the length: "))
  2. unit = input("please input the unit: ")
  3. if unit == "in" or "inch":
  4.     length_b = float(length / 2.54)
  5.     print("%f inch = %f cm"(length, length_b))
  6. elif unit == "cm":
  7.     length_b = float(length * 2.54)
  8.     print("%f cm = %f inch"(length, length_b))
  9. else:
  10.     print("calculation failed")
复制代码

我犯了一个错误:
  1. test.py:143: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
  2.   print("%f inch = %f cm"(length, length_b))
  3. test.py:146: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
  4.   print("%f cm = %f inch"(length, length_b))
复制代码

回答
您必须在打印的报告中使用 %,如下所示:
  1. length = float(input("please input the length: "))
  2. unit = input("please input the unit: ")
  3. if unit == "in" or unit == "inch":
  4.     length_b = float(length / 2.54)
  5.     print("%f inch = %f cm"%(length, round(length_b,6)))
  6. elif unit == "cm":
  7.     length_b = float(length * 2.54)
  8.     print("%f cm = %f inch"%(length, round(length_b,6)))
  9. else:
  10.     print("calculation failed")
复制代码

附加:您可以使用 format() 函数轻松尝试:
  1. length = float(input("please input the length: "))
  2. unit = input("please input the unit: ")
  3. if unit == "in" or unit == "inch":
  4.     length_b = float(length / 2.54)
  5.     print("inch is {} and cm is {}".format(length, length_b))
  6. elif unit == "cm":
  7.     length_b = float(length * 2.54)
  8.     print("cm is {} and inch is {}".format(length, length_b))
  9. else:
  10.     print("calculation failed")
复制代码

输出:
  1. please input the length: 5
  2. please input the unit: in
  3. 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:
  1. length = float(input("please input the length: "))
  2. unit = input("please input the unit: ")
  3. if unit == "in" or "inch":
  4. length_ b = float(length / 2.54)
  5. print("%f inch = %f cm"(length, length_b))
  6. elif unit == "cm":
  7. length_ b = float(length * 2.54)
  8. print("%f cm = %f inch"(length, length_b))
  9. else:
  10. print("calculation failed")
复制代码

I made a mistake:
  1. test. py:143: SyntaxWarning: 'str' object is not callable;  perhaps you missed a comma?
  2. print("%f inch = %f cm"(length, length_b))
  3. test. py:146: SyntaxWarning: 'str' object is not callable;  perhaps you missed a comma?
  4. print("%f cm = %f inch"(length, length_b))
复制代码

answer
You must use% in the printed report as follows:
  1. length = float(input("please input the length: "))
  2. unit = input("please input the unit: ")
  3. if unit == "in" or unit == "inch":
  4. length_ b = float(length / 2.54)
  5. print("%f inch = %f cm"%(length, round(length_b,6)))
  6. elif unit == "cm":
  7. length_ b = float(length * 2.54)
  8. print("%f cm = %f inch"%(length, round(length_b,6)))
  9. else:
  10. print("calculation failed")
复制代码

Additional: you can easily try using the format() function:
  1. length = float(input("please input the length: "))
  2. unit = input("please input the unit: ")
  3. if unit == "in" or unit == "inch":
  4. length_ b = float(length / 2.54)
  5. print("inch is {} and cm is {}".format(length, length_b))
  6. elif unit == "cm":
  7. length_ b = float(length * 2.54)
  8. print("cm is {} and inch is {}".format(length, length_b))
  9. else:
  10. print("calculation failed")
复制代码

Output:
  1. please input the length: 5
  2. please input the unit: in
  3. inch is 5.0 and cm is 1.968504
复制代码

)





上一篇:RecyclerView 未按预期显示数据库内容
下一篇:beginBackgroundTaskWithExpirationHandler 永远不会被调用