Python 3.x Python 3 If语句不返回what';应该是的

Python 3.x Python 3 If语句不返回what';应该是的,python-3.x,if-statement,input,Python 3.x,If Statement,Input,我最近开始学习python,并尝试使用if语句和用户输入。我的代码运行得很好,但现在不行了(我可能是偶然改变了一些东西,但我不知道它可能是什么)。无论我输入什么,控制台总是返回第一条if语句——小时到秒的转换。这是什么原因造成的 print('Welcome to the time to seconds converter') option = input('Please choose what you want to convert ') if option == 'hours' or 'H

我最近开始学习python,并尝试使用if语句和用户输入。我的代码运行得很好,但现在不行了(我可能是偶然改变了一些东西,但我不知道它可能是什么)。无论我输入什么,控制台总是返回第一条if语句——小时到秒的转换。这是什么原因造成的

print('Welcome to the time to seconds converter')
option = input('Please choose what you want to convert ')

if option == 'hours' or 'Hours':
    hours = int(input('How many hours to seconds would you like to convert? '))
    product1 = (hours * 60) * 60
    print(hours, 'hours are', product1, 'seconds')

elif option == 'minutes' or 'Minutes':
    minutes = int(input('Please choose how many minutes to seconds you would like to convert '))
    product2 = minutes * 60
    print(minutes, 'minutes are', product2, 'seconds')

elif option == 'days' or 'Days':
    days = int(input('How many days would you like to convert to seconds? '))
    product3 = ((days * 24) * 60) * 60
    print(product3)

elif option == 'years' or 'Years':
    years = int(input('How many years would you like to convert in to seconds? '))
    product4 = (((years * 365) * 24) * 60) * 60
    print(product4)

else:
    print('Sorry, I do not recognize that')

您的支票需要稍加修改:

if option == 'hours' or 'Hours':
应写明:

if option == 'hours' or option == 'Hours':
幕后发生的事情: 您提到您的代码总是返回第一个if语句的结果。发生这种情况的原因是基于Python的两个特性:

  • Python如何处理
    if
    语句
  • Python如何处理内容字符串与空字符串的真实性测试
让我们看看这两个方面:

Python如何处理if语句 Python将if语句视为两个单独的检查,为了清楚起见,我将使用括号对其进行分段:

if (option == 'hours') or ('Hours'):
在这种情况下,根据您键入的内容,选项可能是“小时”,也可能不是“小时”。如果不是“小时”,那么该测试将返回
False
,Python将继续执行第二次检查,这相当于

if ('Hours'):
检查“Hours”是否始终返回True。这似乎有些奇怪,直到我们考虑到Python如何处理有无内容的字符串。

Python如何处理内容字符串与空字符串的真实性测试 当Python测试一个项是True(truthy)还是False(falsy)时,它使用一些常见的约定:

  • 空容器或字符串通常被认为是错误的 (即,
  • 具有某种形式内容的容器或字符串是 被认为是真实的(即,
    'hello'
    'a'
    'Hours'
所以这是一个测试

if 'Hours':             # returns True
if '':                  # an empty string, returns False
将始终返回True,但这是对

if 'Hours':             # returns True
if '':                  # an empty string, returns False
将始终返回False

顶部建议的代码修订版本相当于:

if (option == 'hours') or (option == 'Hours'):
并针对文本创建两个独立的
选项测试

执行相同操作的其他方法: 有几种方法可以更符合python的方式来处理测试:

if options.lower() == 'hours':
它接受输入并生成输入的小写副本,以针对单个值进行测试。这样做的好处是它涵盖了所有的基础,即如果用户键入小时、小时、小时、小时等)

这将检查选项是否在一组允许的答案中。这样做的好处是,它允许您以后以一种简单易管理的方式添加新选项。i、 e

if options in ['hours', 'Hours', 'HOURS']:
添加
lower()
,以便完全忽略输入的大小写

print('Welcome to the time to seconds converter')
option = input('Please choose what you want to convert ').lower()

if option == 'hours':
    ...

elif option == 'minutes':
    ...

elif option == 'days':
    ...

elif option == 'years':
    ...

else:
    print('Sorry, I do not recognize that')

通过使用.lower()可以使它变得更简单。无论您如何编写,它都会将字符串隐藏在较低的大写字母中

If option.lower()=“小时”