【问题标题】:Python 3 If statement not returning what's supposed toPython 3 If 语句没有返回应该返回的内容
【发布时间】:2021-02-15 18:06:41
【问题描述】:

我最近开始学习 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')

【问题讨论】:

    标签: python-3.x if-statement input


    【解决方案1】:

    您的检查需要稍作修改:

    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'):
    

    检查“小时”是否总是返回 True。这可能看起来很奇怪,直到我们考虑 Python 如何处理有内容和没有内容的字符串。

    Python 如何处理包含内容的字符串与空字符串的真值测试

    当 Python 测试一个项目是 True(真)还是 False(假)时,它使用一些常见的约定:

    • 空容器或字符串通常被认为是 False (即''
    • 具有某种形式内容的容器或字符串是 被认为是真(即'hello''a''Hours'

    所以测试一下

    if 'Hours':             # returns True
    

    将始终返回 True,但测试

    if '':                  # an empty string, returns False
    

    将始终返回 False

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

    if (option == 'hours') or (option == 'Hours'):
    

    并创建两个单独的 options 与文本的测试。

    做同样事情的其他方法:

    有几种方法可以以更 Pythonic 的方式处理您的测试:

    if options.lower() == 'hours':
    

    它接受输入并制作输入的小写副本以针对单个值进行测试。这样做的好处是它涵盖了所有的基础,即如果用户输入 HOURS、Hours、houRs、HOurs 等)

    if options in ['hours', 'Hours']:
    

    这会检查选项是否在一组允许的答案中。这样做的好处是它允许您以后以简单且易于管理的方式添加新选项。即

    if options in ['hours', 'Hours', 'HOURS']:
    

    【讨论】:

      【解决方案2】:

      添加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')
      

      【讨论】:

        【解决方案3】:

        你可以通过使用 .lower() 让它变得更简单,无论你怎么写,它都会用小写字母隐藏字符串

        如果 option.lower()== ‘小时’

        【讨论】:

          猜你喜欢
          • 2019-04-27
          • 2020-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-03
          • 1970-01-01
          • 2018-11-09
          • 2015-12-16
          相关资源
          最近更新 更多