为什么不同的输入总是显示相同的结果
is_male=input("are you male or female?\n")
is_tall=input("are you tall?(Y/N)\n")
male=True
female=False
Y=True
N=False
if is_male and is_tall:
print("you are a tall male")
elif is_male and not(is_tall):
print("you are a short male")
elif not(is_male) and is_tall:
print("you are tall female")
else:
print("you are short female")
当我尝试输入“ female”和“ N”时,结果始终显示“你是个高个子的男性”,我该如何解决?
答案 0 :(得分:0)
# Stores input in is_male
is_male=input("are you male or female?\n")
# Stores input in is_tall
is_tall=input("are you tall?(Y/N)\n")
# Sets a variable male as True
male=True
# Sets a variable female as False
female=False
# Sets variable Y as True
Y=True
# Sets variable N as False
N=False
# Checks is the string is_male is true and is_tall is true, any string is always True
if is_male and is_tall:
print("you are a tall male")
elif is_male and not(is_tall):
print("you are a short male")
elif not(is_male) and is_tall:
print("you are tall female")
else:
print("you are short female")
如上面的代码中所述,除非字符串为“”,否则字符串将被评估为True。因此,在您的代码中,第一个条件将始终为true。
您所做的所有变量分配都是无用的。
# Stores input in is_male
is_male=input("are you male or female?\n")
# Stores input in is_tall
is_tall=input("are you tall?(Y/N)\n")
if is_male == 'Y':
# Code to handle male.
else:
# Code to handle female.
作为练习,您应该填写代码以处理两种情况下的高。
答案 1 :(得分:0)
您正在为所有变量分配字符串值 因此,您必须这样做:
is_male=input("are you male or female?\n")
is_tall=input("are you tall?(Y/N)\n")
if is_male == 'Male' and is_tall == 'Y'
print("you are a tall male")
elif ................ ```