python-3.x - python 3错误不支持&的操作数类型:'str'和'str'
问题描述
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'
请帮我解决一下这个。
解决方案
在 python中使用and
而不是,是按位和 ( https://docs.python.org/3/reference/expressions.html?highlight=bitwise#binary-bitwise-operations )&
&
推荐阅读
- flutter - 如何在逗号 (.) 后只允许 2 位数字和 1 位数字?
- python - 如何在类中创建一个确定圆交点的方法?
- php - 如果模型具有访问器,则带有 Blade 语句的 Laravel 对象值不起作用
- python - 使用 2 级 Python 从 CSV 文件创建嵌套字典
- spring-boot - 如何增加 Vaadin 中的最大文件上传 - Spring Boot
- r - R中数据可视化顺序问题
- c++ - code::blocks 智能感知/自动完成不适用于命名空间
- sql - Oracle Live SQL:SQL 命令未正确结束
- c# - .net 5 项目需要安装 .net core 3.1
- api - 有没有办法通过代码更改 Redragon 产品的 RGB 设置?