python-3.x - IF ELSE 语句在 python 中不能正常工作
问题描述
我是 Python 新手,并试图通过使用if
,else
语句创建一个权重转换器。
这应该要求用户以磅 (lbs) 或千克 (kgs) 为单位输入重量和单位并进行转换,但程序仅执行if
条件。
这是我的代码片段:
weight = int(input("Weight = "))
unit = input('(L)bs or (K)')
if unit == "L" or "l":
print(f'You are {weight*0.45} kilograms')
elif unit == "K" or "k":
print(f'You are {weight*2.2} pounds')
else:
print('The converter is only for Kgs and Lbs')
这是一个屏幕截图:
解决方案
您需要在两行中进行更改:
weight = int(input("Weight: "))
unit = input("(L)bs or (K): ")
# if unit == "L" or "l": # OLD_LINE
if unit == "L" or unit == "l": # NEW_LINE
print(f"You are {weight*0.45} kilograms")
# elif unit == "K" or "k": # OLD_LINE
elif unit == "K" or unit == "k": # NEW_LINE
print(f"You are {weight*2.2} pounds")
else:
print("The converter is only for Kgs and Lbs")
推荐阅读
- python - 如何修改我的代码以便将我从 Python 终端上的代码执行的输出提取到 fasta 文件?
- docker-compose - Docker compose抛出无法执行脚本docker-compose
- javascript - 如何使用存储为字符串的日期查询 mongodb
- python - 无法隔离字符串的元素以进行转换和完成添加
- tensorflow - AttributeError:“JpegImageFile”对象没有“读取”属性
- sql - 如何在 PostgreSQL 10 中更改文件名
- python - 停止列表在父类中是静态的
- java - Grails 唯一域约束
- vue.js - 如何在 vue js 中这样打印?
- powershell - Powershell用单引号调用cmd