Py学习  »  Python

Python中if语句的问题

Rory Green • 3 年前 • 1266 次点击  

我是一名新的python用户,一直在尝试将用户输入集成到一个更简单的代码中。原始代码如下所示。

起初的 ============================ ============== ============== ==============

is_male=True
is_tall=False
if is_male and is_tall:
    print("User is male and tall.")
elif is_male and not(is_tall):
    print("User is male and short.")
elif not(is_male) and is_male:
    print("User is female and tall.")
else:
    print("User is female and short")

这是我想要的方式,但我希望用户能够输入这些信息。然而,我遇到了一个问题,控制台上总是写着“用户是男性且身材高大”我下面的新代码有什么问题?非常感谢你。

修改

is_male=input("You are male. True or False?")
if is_male == "True": 
    is_male == True
elif is_male == "False":
    is_male == False
else:
    print("Please enter True or False.")

is_tall=input("You are tall. True or False?")
if is_tall == "True": 
    is_tall == True
elif is_tall == "False":
    is_tall == False
else:
    print("Please enter True or False.")

is_male=True
is_tall=False
if is_male and is_tall:
    print("User is male and tall.")
elif is_male and not(is_tall):
    print("User is male and short.")
elif not(is_male) and is_male:
    print("User is female and tall.")
else:
    print("User is female and short")
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129345
 
1266 次点击  
文章 [ 2 ]  |  最新文章 3 年前
Ravi Kumar Gupta
Reply   •   1 楼
Ravi Kumar Gupta    3 年前

问题在于作业。你用的是双等号 == 指定一个值而不是一个等号 = .


is_male=input("You are male. True or False?")
if is_male == "True": 
    is_male = True # This should just be single '='
elif is_male == "False":
    is_male = False # This should just be single '='
else:
    print("Please enter True or False.")

is_tall=input("You are tall. True or False?")
if is_tall == "True": 
    is_tall = True # This should just be single '='
elif is_tall == "False":
    is_tall = False # This should just be single '='
else:
    print("Please enter True or False.")

除此之外,我认为这些行只是错误地再次添加。请把那些拿走-

is_male=True
is_tall=False

最后,你的比较应该是-

if is_male and is_tall:
    print("User is male and tall.")
elif is_male and ( not is_tall):
    print("User is male and short.")
elif (not is_male) and is_tall: # fix the typo here.. 
    print("User is female and tall.")
else:
    print("User is female and short")

第二 elif 你在测试 is_male 再次而不是 is_tall .

mozway
Reply   •   2 楼
mozway    3 年前

您应该删除以下行:

is_male=True
is_tall=False

它们会覆盖用户选择的任何内容,从而使您的所有输入无效;)