我需要 python 在输入 1 时理解 True,是的,键入 y
I need python understand True when 1, yea, y is typed
我想了解 运行 下面 Python 代码的正确方法。我想评论将完成下面的工作。
现在,这仅在我输入首字母大写的 "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
您可以像这样使用真实条件列表:
name = raw_input ("What is your name human?: ")
print "Got it! Your name is %r" % name
computers_owner = raw_input("Are you the owner of this machine?: ")
if computers_owner in ['1','yea','y','yo','hell yeah','true','True', 'true dat']:
print "Good! I need you %s" % name
else:
print "Call me your master! %s" % name
我想了解 运行 下面 Python 代码的正确方法。我想评论将完成下面的工作。 现在,这仅在我输入首字母大写的 "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
您可以像这样使用真实条件列表:
name = raw_input ("What is your name human?: ")
print "Got it! Your name is %r" % name
computers_owner = raw_input("Are you the owner of this machine?: ")
if computers_owner in ['1','yea','y','yo','hell yeah','true','True', 'true dat']:
print "Good! I need you %s" % name
else:
print "Call me your master! %s" % name