2017-05-26 1 views
-1

私はヒーローが10秒で2人の人質を保存するプログラムを作成しています。彼が最初に必要とすることは、最初に誰を保存するかを決めることです。その後、人質を救出するための爆弾のコードを入力します。しかし、いったんループを止めたり、最初の人質を保存した後に間違って入力すると、ループが止まります。'for for'はraw_inputの後で動作しなくなる

これはコードです:

holly = True 
molly = True 

for i in range(0,11): 
    print "Who will you save?" 
    print "Time paste:", i 

    decide = raw_input("> ") 

    if decide == "Holly" and holly: 
     print "You decided to save Holly." 
     print "Whats the code?" 
     code = random.randint(100,1000) 
     print code 
     decide = int(raw_input("> ")) 
     if decide != code: 
      exit('You are dead!') 
     print "You saved Holly!" 
     holly = False 
    elif decide == "Molly" and molly: 
     print "You decided to save Molly." 
     print "Whats the code?" 
     code = random.randint(100,1000) 
     print code 
     decide = int(raw_input("> ")) 
     if decide != code: 
      exit('You are dead!') 
     print "You saved Molly!" 
     molly = False 
    elif not holly and not molly: 
     print "You saved both of them!" 
     break 
    else: 
     print "Try again!" 

私はクラスでそれをすべてやって、ここでの完全なコードは次のとおりです。

class Mountains(object): 
    def enter(self): 
     print "After you defeated Josh. You headed towards the mountains." 
     print "There you're final target sits." 
     print "Jim the \"Knigh\"" 
     print "He has 2 hostages. Holly and Molly, you have 10 seconds to save them." 
     print "They are bought straped to a bomb, each one in different location" 
     print "Each bomb has a 3 digit code." 

     holly = True 
     molly = True 

     for i in range(0,11): 
      print "Who will you save?" 
      print "Time remaining:", i 

      decide = raw_input("> ") 

      if decide == "Holly" and holly: 
       print "You decided to save Holly." 
       print "Whats the code?" 
       code = random.randint(100,1000) 
       print code 
       decide = int(raw_input("> ")) 
       if decide != code: 
        exit('You are dead!') 
       print "You saved Holly!" 
       holly = False 
      elif decide == "Molly" and molly: 
       print "You decided to save Molly." 
       print "Whats the code?" 
       code = random.randint(100,1000) 
       print code 
       decide = int(raw_input("> ")) 
       if decide != code: 
        exit('You are dead!') 
       print "You saved Molly!" 
       molly = False 
      elif not holly and not molly: 
       print "You saved both of them!" 
       break 
      else: 
       print "Try again!" 

      if holly and molly: 
       exit("You are dead!") 

      print "After you saved both, Jim comes in angry!" 
      print "He takes his shotgun! And startes firing!" 
      print "You take cover, and start to think where to shoot!" 
      print "You only have 4 bullets! Make it count!" 

      hero = 100 
      villain = 100 
      bullets = 4 
      direction = ["up", "down", "left", "right"] 

      while True: 
       print "In which direction do you shoot?" 
       print "Hero health:",hero 
       print "Villain health:",villain 
       print "Bullets left:", bullets 

       if hero < 0: 
        exit("You are dead.") 
       elif villain < 0: 
        exit("You win") 
       elif bullets == 0: 
        exit("You are dead.") 

       bullets -= 1 
       shoot = random.choice(direction) 
       print shoot 

       choice = raw_input("> ") 

       if choice == shoot: 
        print "You shoot %s, hitting Jim but he also hits you." % shoot 
        hero -= random.randint(20,30) 
        villain -= random.randint(30,50) 
       elif choice != shoot: 
        print "You shoot %s, but you missed. Jim didn't miss." % shoot 
        hero -= random.randint(10,20) 
       else: 
        print "Try again." 
     else: 
      print "Try again" 
+2

私は問題を再現できません。コードはまさにそれがやっているように見えます。 – JacobIRR

+0

コードは別々に正常に動作します。しかし、クラスではそうではありません。 –

+0

クラス内のコードをどのように使用していますか?あなたの答えを編集して、その部分を表示してください。 – martineau

答えて

0

私は継続使用して問題を解決しました。これらは、私が挿入した部分です:

if decide == "Holly" and holly: 
    print "You decided to save Holly." 
    print "Whats the code?" 
    code = random.randint(100,1000) 
    print code 
    decide = int(raw_input("> ")) 
    if decide != code: 
     exit('You are dead!') 
    print "You saved Holly!" 
    holly = False 
    continue 
elif decide == "Molly" and molly: 
    print "You decided to save Molly." 
    print "Whats the code?" 
    code = random.randint(100,1000) 
    print code 
    decide = int(raw_input("> ")) 
    if decide != code: 
     exit('You are dead!') 
    print "You saved Molly!" 
    molly = False 
    continue 
else: 
    print "Try again!" 
    continue 
関連する問題