【发布时间】:2017-01-02 19:23:59
【问题描述】:
我想了解如何以正确的方式在 Python 代码下运行。我猜 cmets 会做下面的工作。 现在,这仅在我用大写首字母键入“True”或“False”的情况下才有效。但我也需要在输入 1 或 true 等时得到相同的结果。
# Here we ask the user place an input and assign it to name parameter.
name = raw_input ("What is your name human?: ")
# Here we will display the value of name parameter placed by user
print "Got it! Your name is %r" % name
# Here we ask the user if he is owning the device.
# We will assign the answer to computers_owner parameter
computers_owner = input ("Are you the owner of this machine?: ")
# In this question's answer we have an if condition.
# If the answer is True machine will say "Good! I need you" % name
if computers_owner is True:
print "Good! I need you %s" % name
# If the answer is False machine will say "Call me your master!" % name
else:
print "Call me your master! %s" % name
【问题讨论】:
-
在第二个输入中也使用
raw_input()并像这样比较:computers_owner.lower() == "true" -
并添加更多您提到的条件
computers_owner.lower() == 'true' or computers_owner.lower() == 'y' or computers_owner == 1。你明白了。 -
为什么用户希望能够输入
1作为是/否问题的答案?只需检查以y或n开头的字符串(忽略大小写)并完成。
标签: python boolean boolean-logic boolean-expression