python - python中使用OR运算符进行温度转换

标签 python

我尝试使用此代码在摄氏温度和华氏温度之间制作一个简单的温度转换器......我的代码:

value=raw_input("Temperature Reading= ")
check=value[-1]
c=int(value[:-1])
if check =='c' or 'C':
    print (9*c/5)+32,'F'
if check =='f' or 'F':
    print 5*(c-32)/9,'C'

raw_input("Press <Enter> to exit!")

问题在于,如果用户输入例如 50f,它就会在“if”函数中打印它们。我需要纠正它。谢谢:)

最佳答案

您的支票不正确

if check =='f' or 'F':

应该是

if check == 'f' or check == 'F':

另一个“C”的同上。

更好的是:

if check in ['c', 'C']:

或者

if check.lower() == 'c':

关于python - python中使用OR运算符进行温度转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22793799/

相关文章:

python - 替换张量中大于零的所有值

Python int 太大,无法在 Python COM 中转换为 C long

python - 在 Python 中将类实现为字典

python - Ansible Community.aws.aws_ssm 模块。错误!一名 worker 被发现已死亡

python - 如何通过 Django ORM 排除查询中的两个条件?

python.NLTK(WindowDiff 和 PK)与 python.Segeval(WindowDiff 和 PK)

python - Django,如何生成没有模型的管理面板?

python - Django 模型按人类可读的值进行过滤

Pythonistas,请帮助将其转换为利用 Python 线程概念

从另一个文件返回函数时的 Python 问题