2016-07-30 12 views
1
import time 
import random 
import sys 

tries = 1 

def start(): 
    global tries 
    tries = 1 
    global chest1 
    chest1 = random.sample(xrange(1, 20), 1) 
    chest1==str 
    global chest2 
    chest2 = random.sample(xrange(1, 20), 1) 
    chest2==str 
    global chest3 
    chest3 = random.sample(xrange(1, 20), 1) 
    chest3==str 
    print chest1 
    while chest1 == chest2 or chest1 == chest3 or chest2 == chest3: 
     start() 
    else: 
     print"Alright lets begin!" 
     game() 

def defeat(): 
    time.sleep(3) 
    end = raw_input("Would you like to start again(yes or no)?") 
    if end == "yes": 
     start() 
    if end == "no": 
     print"Goodbye!" 
     time.sleep(1); 
     print"Shutting Down" 
     time.sleep(2) 
     sys.exit() 
    else: 
     print"Please input a valid answer" 

def game(): 
    global chest1 
    global chest2 
    global chest3 
    print chest1, chest2, chest3 
    num = input("Choose a chest from 1-20!") 
    if num == chest1 or num == chest2 or num == chest3: 
     print "Well Done! Get another chest to move on to Sudden Death!" 
    else: 
     while (tries < 3): 
      global tries 
      print"Try Again" 
      tries = tries + 1 
      game() 
     else: 
      print "You've taken too many tries.. YOU DIE!" 
      defeat() 

私はif文に到着すると、このコードを実行する:文が認識されない場合は、[ゲームで()関数]

if num == chest1 or num == chest2 or num == chest3: 
    print "Well Done! Get another chest to move on to Sudden Death!" 

と私は知っている(私の入力と胸のいずれかと一致私がそれを印刷させてから選択した数字)、それはelse文にジャンプし、正しい入力を胸に一致させても「再試行する」と言います。私は何時間もこれをしています...助けてください。ありがとうございました!

答えて

1

問題のカップルは、少なくともあります

  1. あなたはchest1chest2chest3にリストを割り当てています。
  2. 入力は文字列で、intを持つ1要素のリストと比較しています。

代わりにこれを試してみてください:

import time 
import random 
import sys 

tries = 1 

def start(): 
    global tries 
    tries = 1 
    global chest1 
    chest1 = random.randint(1, 20) 
    global chest2 
    chest2 = random.randint(1, 20) 
    global chest3 
    chest3 = random.randint(1, 20) 
    while chest1 == chest2 or chest1 == chest3 or chest2 == chest3: 
     start() 
    else: 
     print("Alright lets begin!") 
     game() 

def defeat(): 
    time.sleep(3) 
    end = raw_input("Would you like to start again(yes or no)?") 
    if end == "yes": 
     start() 
    if end == "no": 
     print("Goodbye!") 
     time.sleep(1); 
     print("Shutting Down") 
     time.sleep(2) 
     sys.exit() 
    else: 
     print("Please input a valid answer") 

def game(): 
    global chest1 
    global chest2 
    global chest3 
    print(chest1, chest2, chest3) 
    num = int(raw_input("Choose a chest from 1-20!")) 
    if num == chest1 or num == chest2 or num == chest3: 
     print("Well Done! Get another chest to move on to Sudden Death!") 
    else: 
     while (tries < 3): 
      global tries 
      print("Try Again") 
      tries = tries + 1 
      game() 
     else: 
      print("You've taken too many tries.. YOU DIE!") 
      defeat() 

編集を:また、一般的に眉をひそめるされたグローバルを使用しました。私はあなたのコードをリファクタリングしてそれらを使用しない自由を取った:

import time, random, sys 

def start(): 
    chest1 = random.randint(1, 20) 
    chest2 = random.randint(1, 20) 
    chest3 = random.randint(1, 20) 
    tries = 1 
    while chest1 == chest2 or chest1 == chest3 or chest2 == chest3: 
     start() 
    else: 
     print("Alright lets begin!") 
     game(chest1,chest2,chest3,tries=tries) 
    return tries 

def defeat(): 
    time.sleep(3) 
    end = raw_input("Would you like to start again(yes or no)?") 
    if end == "yes": 
     tries = start() 
    if end == "no": 
     print("Goodbye!") 
     print("Shutting Down") 
     sys.exit() 
    else: 
     print("Please input a valid answer") 

def game(chest1,chest2,chest3,tries=None): 
    print(chest1, chest2, chest3) 
    num = int(raw_input("Choose a chest from 1-20!")) 
    if num == chest1 or num == chest2 or num == chest3: 
     print("Well Done! Get another chest to move on to Sudden Death!") 
     while (tries < 3): 
      tries += 1 
      game(chest1,chest2,chest3,tries=tries) 
     else: 
      print("Moving to Sudden Death!") 
      sudden_death(chest1,chest2,chest3,tries=tries) 
    else: 
     while (tries < 3): 
      print("Try Again") 
      tries += 1 
      game(chest1,chest2,chest3,tries=tries) 
     else: 
      print("You've taken too many tries.. YOU DIE!") 
      defeat() 

def sudden_death(chest1,chest2,chest3,tries=None): 
    print("Sudden Death not yet implemented; exiting") 
    defeat() 
+0

いいえ、できます。ありがとうございました! – Oinkers

+0

大歓迎です。 – bernie

関連する問題