2017-07-20 53 views
0
print("enter your age") 
age = int(input()) 
if age < 21: 
    print("no beer") 
if age > 21: 
    print("what beer do you like?") 
beer = input() 
if beer == "union": 
    print("this is water") 
if beer == "jelen": 
    print("great") 
else: 
    print("aren't you drinking ? ") 

键入union后,它还运行其他函数。为什么?else函数运行但它不应该

+1

您需要提供更多的信息。其他人会准时运行,所以你的情况一定是错的。你需要说出你的期望。 – Carcigenicate

+0

检查啤酒类型(啤酒)也打印它 –

+0

@Carcigenicate问题的标题字面上说'else'分支 - 只有一个在代码示例 - 不应该运行。这是预期的行为。 – millimoose

回答

2

你缺少一个elif

if beer == "union": 
    print("this is water") 
elif beer == "jelen": 
    print("great") 
else: 
    print("aren't you drinking?") 

否则你的两个if小号获得单独解释:

# first condition 
if beer == "union": 
    print("this is water") 
# end first condition 

# second condition 
if beer == "jelen": # beer is 'union', this branch doesn't run 
    print("great") 
else: # beer is not 'jelen', so this branch runs 
    print("aren't you drinking ? ") 
# end second condition 
相关问题