【问题标题】:Boolean Inputs in PythonPython 中的布尔输入
【发布时间】:2019-03-12 05:45:15
【问题描述】:

我正在尝试解决这个布尔输入问题,但我无法找到答案。它是这样说的,我们传入 2 个布尔输入,寒冷和下雨。

你应该输出一个字符串: ('冷'或'暖')'和'('下雨'或'干燥') 基于这些输入。

('cold' or 'warm') 表示您应该使用两个词中的一个,具体取决于输入的布尔值。

例如 False,True = '温暖多雨'

这是我的代码:

isCold= sys.argv[1] == 'True'
isRainy= sys.argv[2] == 'True'

if isCold:
  print('cold and rainy')

elif isRainy:
  print('warm and rainy')

else:
  print(cold and dry)

我不知道我能做些什么来解决这个问题。

【问题讨论】:

  • 请添加更多信息。
  • 你传递什么作为参数?
  • 你得到的输出是什么?
  • 预期输出:cold and dry 你的程序输出:cold and rainy

标签: python boolean


【解决方案1】:

试试这个代码:

import sys


def func1(isCold, isRainy):
    s = ''
    if isCold:
        s = s + 'cold and '
    else:
        s = s + 'warm and '

    if isRainy:
      s = s + 'rainy'
    else:
     s = s + 'dry'

    print(s)

isCold= sys.argv[1] == 'True'
isRainy= sys.argv[2] == 'True'

func1(isCold, isRainy)

然后,在您的 linux 终端中使用以下命令运行它:

python3 your_file_name.py False True

或者,如果您是 Windows 用户:

your_file_name.py False True

【讨论】:

    【解决方案2】:

    我能想到的最简洁的答案是:

    temperature_description = "warm" if isWarm else "cold"
    rain_description = "rainy" if isRainy else "dry"
    print "{} and {}".format(temperature_description, rain_description)
    

    【讨论】:

      【解决方案3】:

      感谢大家帮助我找到答案。

      【讨论】:

        猜你喜欢
        • 2018-07-26
        • 1970-01-01
        • 2014-11-29
        • 2015-06-20
        • 2015-10-02
        • 2021-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多