本地变量' fight_run'在分配之前引用

时间:2015-10-08 18:32:19

标签: python loops indentation

我一直在环顾四周,但我一直无法修复此错误。 在此错误之前,程序总是打印你的一周是平均没有发生什么事情,另外两个可能发生的变量没有'

这一切都在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()        

2 个答案:

答案 0 :(得分: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 :(得分:1)

如果战斗的价值是1,该怎么办?

然后if fight == 3为false,因此会跳过变量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块的一部分。

我还会考虑使用elifelse并且可能将输入放在循环中,而不是长序列的“if”。如果我输入&#34;撒尿裤子怎么办?而不是&#34;运行&#34;或&#34;战斗&#34;?