2016-07-07 7 views
-2

最後の2つのelif文は機能しません。前の2つのいずれかにリダイレクトされます。そして、elseステートメントでは、ポイント数を表示しません。このコードはなぜ機能しないのですか?ありがとう!下の2つのelse if文が実行されないのはなぜですか?

def match_duel(): 

    print "do you want to swing right or left" 
    print "and for how many points?" 

    choice = raw_input("> ") 
    points = 0 
    damage = 0 

    if "right" in choice: 
     print "you take a swing right. 20 points." 
     points = points + 20 
     print "You have %d points" % points 
     match_duel() 
    elif "left" in choice: 
     print "you take a swing left. 50 points." 
     points = points + 50 
     print "You have %d points" % points 
     match_duel() 
    elif "hard right" in choice: 
     print "you take a hard swing right for 100 points." 
     print "cthulhu strikes back with 100 points." 
     print "you take half the damage." 
     points = points + 100 
     damage = damage - 50 
     print "You have %d points" % points 
     print "You have %d damage" % damage 
     match_duel() 
    elif "hard left" in choice: 
     print "you take a hard swing left for 200 points." 
     print "cthulhu strikes back with 50 points, taken by surprise." 
     print "you take half the damage" 
     points = points + 200 
     damage = damage - 25 
     print "You have %d points" % points 
     print "You have %d damage" % damage 
     match_duel() 
    else: 
     print "take an extended hit." 
     points = points + 300 
     print "You have %d points" % points 
+11

考えてみましょう: ''選択肢の中で '' left ''の値が何よりも ''難しい ''が残っている場合は?したがって、どの状態が先に進むべきですか? – Bakuriu

+0

シンプルな '' left ''をテストする前に' 'hard left' '*をテストしてください。 – usr2564301

+0

右。コードが立っているので、最後の2つの節のいずれにも到達することはできません。 – Prune

答えて

0

ここではelifというステートメントのみ実行できます。条件を満たす最初のものが実行され、残りは無視されます。

"left" いつもは何でも問題ありません。 「厳しい権利」と同じです。そのため、下部の2 elif文は決して実行されません。

関連する問題