[软件设计/软件工程] 在 Python 中出现错误“SyntaxWarning: 'str' object is not callable”

[复制链接]
发表于 2022-5-3 09:33:20
问题
我正在学习 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 永远不会被调用

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表