来自raw_input的if,elif,else响应问题

时间:2015-07-16 00:54:55

标签: python loops if-statement for-loop

所以,在这段代码中,我让用户输入一个字符串。用户必须回答给定的选项之一(在for循环中显示)。如果用户没有响应任何选项,则应使用else语句,重新启动该块。但是,无论choice1的输入是什么,都始终使用if语句。我不确定if,elif,else语句是怎么回事。

print 'You enter door 1 and find a hideous internet troll.'
print "The troll says, 'LOOK AT THIS LOSER"
options = ["YOU'RE THE LOSER", "I don't care." , "YOU'RE FAT"]
for x in options:
    print "\t :> %s" % x
choice1 = raw_input("What is your response? :>")
if 'loser' or 'fat' in choice1:
    print "You are sucked into the troll's behaviour and are driven insane"
    print "After three days of insanity, you starve to death"
    dead()
elif 'care' in choice1:
    print 'The troll cannot feed off of your anger.'
    print 'The troll starves and you recover one piece of the lever from his stomach.'
    change()
    entrywayopen()
    return incomp1 == True
else:
    unknown()
    change()
    door1()

1 个答案:

答案 0 :(得分:1)

在表达式if 'loser' or 'fat' in choice1:中,只检查'fat'是否在select1中。 'loser',是一个非空字符串,只是被解释为True,所以你if True or 'fat' in choice1总是产生True。请改为if ('loser' in choice1) or ('fat' in choice1):