Gary8682 :
Learning python. and I got an error, searched online but I still don't understand why. can somebody help?
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")
and I got this error:
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))
thanks~
Sheri :
You have to use % in your print statements like this:
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")
Bonus:You can use format()
function easily try this:
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
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=398194&siteId=1