2015-12-08 36 views
-3
from random import randint 
from time import sleep 

fight = False 

def start_up(): 
    print("Fight Simulator - Test Stages.") 
    userInput = str(input("Enter your name ")) 
    user_name = userInput 
    return user_name 

def intro(): 
    userName = start_up() 
    while True: 
     userInput = str(input("Welcome to the dojo, {}. To commence the first battle, type 'Start', or 'Begin'".format(userName))) 
     if userInput == "Start" or "start" or "Begin" or "begin": 
      return userInput 
     else: 
      print("Enter a valid response") 
      break 

def fight_commence(): 
    userInput = intro() 

    if userInput == "Start" or "start" or "Begin" or "begin": 
     fight = True 

     userHp = 100 
     opponentHp = 100 

     while fight == True: 
      userDmg = randint(0,100) 
      opponentDmg = randint(0,100) 

      opponentHp -= userDmg 

      if opponentHp <= 0: 
       opponentHp == 0 

      print("You did {} damage".format(userDmg)) 
      sleep(1) 
      print("Opponent has {} hp remaining".format(opponentHp)) 
      sleep(1) 

      if opponentHp <= 0: 
       print("You killed him!") 
       fight = False 
    else: 
     print("Enter a valid action") 

fight_commence() 

因此,出於某種原因,當給出「開始」或「開始」或「開始」或「開始」以外的響應時,代碼不會打印(「輸入有效響應」)。我希望它循環回到用戶輸入階段,但它只是繼續執行程序,就好像一切正​​常。我無法弄清楚我弄亂了什麼。到目前爲止我的代碼有兩個問題。

其次,我希望opponentHp自動等於0時0以下。例如,該健康驟降時opponentHp = -45,opponentHp應等於0。

任何幫助理解。

+1

可能會幫助你,如果你標記的語言.... – epascarello

+0

廢話,它的蟒蛇。 –

+4

「我的代碼到目前爲止的兩個問題」並不是一個非常具體的/可搜索的標題。你的問題是一個經典的問題:'如果userInput ==「開始」或「開始」或「開始」或「開始」:'沒有做你認爲的事情......(提示:用戶輸入==「開始」)或「開始」或「開始」或「開始」:') –

回答

-1

條件(if-clause)是錯誤的。您應該每or重複'userInput =='。 或者更簡潔的:

userInput.lower() in ["start", "begin"] 
+1

你可以編輯來指出這應該是'if'子句 – Tgsmith61591

1

很好用的「有效投入」的測試的一個大問題是你的測試方式。

if userInput == "Start" or "start" or "Begin" or "begin" 

這不是有效的python條件。相反,做這樣的事情:

if userInput in ["Start", "start", "Begin", "begin"] 

這種比較發生兩次,是不好的。對於opponenthp0,你現在有這個作爲你的說法和結果:

if opponentHp <= 0: 
    opponentHp == 0 

的條件是完全正常的,但在行動==是另一個條件運算符,則需要將其更改爲:

if opponentHp <= 0: 
    opponentHp = 0 

加成 此外,除了上述項目,當您使用break語句,你會結束了,你已經設置了無限循環,並最終沒有返回值的函數,你可以婉請考慮將其更改爲continuepass聲明,或者直接將其刪除。