2016-07-30 17 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語句不被認可[在遊戲()函數]

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將它與一個元素列表進行比較。

試試這個:

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