gpt4 book ai didi

python - 在 Python 中是否有更简洁的方法来编写此 bool 比较?

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:40 25 4
gpt4 key购买 nike

<分区>

这个作业的说明是:

We pass in 2 boolean inputs, cold and rainy.

You should output a single string: ('cold' or 'warm') ' and ' ('rainy' or 'dry') based on these inputs.

('cold' or 'warm') means you should use on of the two words, depending on the input boolean value.

for example False, True = 'warm and rainy'

我输入的代码是:

# Get our boolean values from the command line
import sys
isCold= sys.argv[1] == 'True'
isRainy= sys.argv[2] == 'True'

# Your code goes here

condition = ""
if (isCold):
condition += "cold"
else:
condition += "warm"

if (isRainy):
condition += " and rainy"
else:
condition += " and dry"

print(condition)

代码是正确的并且输出了它应该输出的内容,但我想知道是否有更简洁的方式来编写它?我觉得有,但我不太明白。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com