2015-10-08 29 views
1

我一直在四處查看,但是一直無法解決此錯誤。 也作爲一個方面的問題這個錯誤之前,該計劃將始終打印星期的平均水平沒有發生什麼問題,可能的發生的其他2個變量沒有在分配之前引用的局部變量'fight_run'

這是所有在Python

from random import randint 
import time 
money = 2000 
week = 0 
def stockmarket(): 
    global money 
    global week 
    stock = int(randint(1,50)) 
    fight_random = int(randint(1,4)) 
    fight = int(randint(1,100)) 
    gain_lose = int(randint(1,2)) 
    win_lose_var = int(randint(1,30)) 
    luck = int(0) 
    if money > 10000 : 
     print ("""congratulations you beat the game by earning 10,000 
     now that you have so much money you can go back to your life as a hip hop artist """) 
     time.sleep(4) 
     print("it took you ",week,"weeks's to compleate the game") 
     print("can you do it faster next time") 
    else: 
     print(" you have gained lots of money",money,"") 
     print("you must now invest it all the stock market") 
     human_stock = int(input("please pick a number between 1-100 not that it matters as the markets are rigged: ")) 
      #need to make the user number matter 
     change1 = int(stock-40+luck) 
     change2 = float (change1/100) 
     change3 = float (1-change2) 
     money = money * change3 
     week = week+1 
     print ("you have" , money,"") 
     week = week +1 


#THIS IS WHERE THE PROBLEM STARTS! 
     if fight == 3: 
      print("bad luck") 
      fight_run = str(input("""late at night you get approched by a bunch of bad, bad people. they attempt to mugg you but you see there is a chance to run away but you could fight 
         them and gain lots of money! do you fight or run""")) 
     if fight_run == "run": 
       print("you fled the scene but left some money behind which has been stolen.") 
       money *=.8 
       print(" ",money," ") 
       stockmarket() 
     if fight_run == "fight" : 
       print ("you chose to fight the oncoming enemys!") 
     if fight < 0 or fight > 11: 
        print("you where over powered by your enemys and died") 
        time.sleep(5) 
        quit() 
     elif fight <10 and fight >80 : 
        win_lose = int(input("the fight is close and you are both weak. /n please pick and number between 1 and 30 for a chance to gain money!")) 
     elif gain_lose == 1: 
       print("you deafeated your attacckers and take there money (its not a crime if no one saw)") 
       money_fight_win = (win_lose/100)+ 1 
       money = money * money_fight_win 
       print ("",money,"") 
     elif gain_lose == 2 : 
       print ("your attacker won and they took your money luckly you hide some just before the fight ") 
       money_fight_lose = (win_lose/100)+ 1 - (winlose/50) 
       money = money * money_fight_lose 

     else : 
       print("you mortaliy wounded the atackers (cause ur dench m8) and took their money") 
       money = money *1.5 
       #loop 
       stockmarket() 

     if fight == 4: 
      print ("you found a lucky penny and added it to your penny collection") 
      luck = +1 
     elif fight == 1 or 2: 
      print("your week was average nothing much happened") 
      #loop 
      stockmarket() 


#gets program to start 
stockmarket()   

回答

1

你直接問題是縮進級別。所以,如果戰鬥!= 3,那麼fight_run永遠不會被聲明或設置。所以當你在下一個if語句中檢查fight_run時,它有可能不存在。修正你的縮進,讓它讀取。

if fight == 3: 
    print("bad luck") 
    fight_run = str(input("""late at night you get approched by a bunch of bad, bad people. they attempt to mugg you but you see there is a chance to run away but you could fight them and gain lots of money! do you fight or run""")) 
    if fight_run == "run": 
     print("you fled the scene but left some money behind which has been stolen.") 
     money *=.8 
     print(" ",money," ") 
     stockmarket() 
    if fight_run == "fight" : 
     print ("you chose to fight the oncoming enemys!") 

這樣,如果戰鬥== 3,則接近玩家,然後根據玩家的選擇他可以戰鬥或跑步。解決這個問題會讓你接觸到下一個錯誤,你的代碼有很多問題,包括像這樣的更多縮進級別的問題。

關於您提到的另一個問題,當您執行elif fight == 1 or 2:時,這將始終爲真,因爲測試2作爲布爾值將始終爲真。這將需要是elif fight == 1 or fight == 2:

另外,正如羅伯特在他的回答中所說的那樣,在這裏用戶可以輸入一個不匹配任何檢查的指令的情況下有幾種情況。你應該總是確認你的指導不能通過而不做任何事情。

+1

我剛剛刪除了一個類似的答案,因爲這個更清晰。另外,如果我輸入「尿褲子」而不是「跑步」或「打架」呢? – RobertB

+0

儘管您已經對輸入驗證做了一個很好的說明。他目前不執行互斥輸入檢查或驗證輸入是可消化的。 – saarrrr

+0

好的,我刪除了我的刪除答案,即使有很多重疊。 :) – RobertB

1

如果戰鬥值是1,該怎麼辦?

然後if fight == 3是錯誤的,所以變量fight_run的創建被跳過,所以變量fight_run不存在。那麼你正在測試if fight_run == "run",你被告知fight_run不存在。

我想你想要做的是縮進代碼:

if fight_run == "run": 
    print("you fled the scene but left some money behind which has been stolen.") 
    money *=.8 
    print(" ",money," ") 
    stockmarket() 
if fight_run == "fight" : 
    print ("you chose to fight the oncoming enemys!") 

所以它是if fight == 3塊的一部分。

取而代之的是'if'的長序列,我還會考慮使用elifelse,並且可能會將輸入置於循環中。如果我輸入「尿褲子」而不是「跑步」或「打架」呢?

相關問題