def temperature():
A=str(input("enter your unit"))
B=str(input("enter your conversion unit"))
C=int(input("enter temperature"))
if A=="c" & B=="f":
print("your temp in fahrenheit is",C*9/5+32,"f")
elif A=="c" & B=="k":
print("your temp in kelvin is",C+273.15,"k")
elif A=="f" & B=="c":
print("your temp in celsius is",C-32*5/9,"c")
elif A=="f" & B=="k":
print("your temp in kelvin is",C+459.67,"k")
elif A=="k" & B=="c":
print("your temp in celsius is",C+273.15,"c")
elif A=="k" & B=="f":
print("your temp in fahrenheit is",C-273.15*9/5+32,"f")
else:
print("not valid")
temperature()
当我运行此代码时,它将引发以下错误:
File "D:\PYTHON PROGRAMS\if and else\temperature.py", line 20, in <module>
temperature()
File "D:\PYTHON PROGRAMS\if and else\temperature.py", line 5, in temperature
if A=="c" & B=="f":
TypeError: unsupported operand type(s) for &: 'str' and 'str'
请帮助我。
答案 0 :(得分:0)
使用and
代替&
,在python中&
是按位的(https://docs.python.org/3/reference/expressions.html?highlight=bitwise#binary-bitwise-operations)