这个简单的python代码不起作用?

时间:2014-06-01 09:55:23

标签: python maze

每当输出导致盾牌更改时,程序只会说“你有5个盾牌”,然后是“你赢”。我认为在将变量从一个函数传递到另一个函数时,我可能犯了一个错误。提前感谢您的帮助!

def main():
    while TakeTurn(word,shield) == False:
        if shield == 1:
            word1 = "shield"
        else:
            word1 = "shields"
        print ("You have", shield, word1,)
    if shield < 1:
        print ("Sorry! You ran out of shields! You lose!")
    else:
        print ("You win")
#This function is the actual 'game' and will deterine what happens to the character    
def TakeTurn(word1,shield1):
    time.sleep(1.5)
    #This means that when the user reaches 0 shields, they lose.
    if shield1 < 1:
        return True
    #Whatever the user inputs will not actually affect the outcome
    print ("You have reached", word1 ,"junction.\nDo you want to turn left (L), turn right (R) or go straight ahead(S)?")
    turning = input()
    #This is a simple instruction that means that the first time you come to a junction, it will say 'a junction' but the second time it will say 'another junction'
    word1 = "another"
    #This 'if' statement means that the program will only continue if the user has inputed the letters 'L', 'R' or 'S'
    elif turning not in ["L","R","S","l","r","s"] :
        time.sleep (0.7)
        print ("Sorry, I didn't understand that")
        TakeTurn()
    else:
        choice = randint (1,10)
    #This is just going to display a random message which will affect the outcome
    time.sleep (1)
    if choice == 1:
        print ("You have found the exit!")
        return True
    elif choice == 2:
        print ("You have found a shield!")
        time.sleep(1)
        shield1 = shield1 +1
        return False
    elif choice == 3:
        print ("You have found two shields!")
        time.sleep(1)
        shield1 = shield1 +2
        return False
    elif choice == 4:
        print ("You have found three shields!")
        time.sleep(1)
        shield1 = shield1 +3
        return False
    elif choice == 5:
        print ("A fairy has jumped into your pants!")
        time.sleep(2)
        print ("You lose two shields")
        time.sleep(1)
        shield1 = shield1 -2
        return False
    elif choice == 6:
        treasurechest(shield1)
        return False
    elif choice == 7:
        print ("You have tripped over a log!")
        time.sleep(2)
        print ("You lose a shield")
        time.sleep(1)
        shield1 = shield1 -1
        return False
    elif choice == 8:
        print ("An angry teenager is staring at you in the eye.")
        time.sleep(2.5)
        print ("He uses laziness...")
        time.sleep(1.5)
        print ("It's super effective!")
        time.sleep(1)
        print ("You lose three shields")
        time.sleep(1)
        shield1 = shield1 -3
        return False
    elif choice == 9:
        print ("You have encountered an ogre...")
        time.sleep(1.5)
        print ("He bashes you over the head with a steel bar")
        time.sleep(2)
        print ("You lose two shields")
        time.sleep(1)
        shield1 = shield1 -2
        return False
    else:
        print ("A goblin aproaches and says the following:")
        time.sleep(2)
        goblin(shield1)
        return False

1 个答案:

答案 0 :(得分:2)

您应该重构mainTakeTurn以使shieldword显式参数和return值。这可以防止依赖于访问变量的范围,而不必使用global(这通常是一个坏兆头):

def main(shield, word):
    while True:
        shield, word, finished = TakeTurn(shield, word)
        if finished:
            break
        word1 = "shield" if shield == 1 else "shields"
        print ("You have {0} {1}".format(shield, word1))
    if shield < 1:
        print ("Sorry! You ran out of shields! You lose!")
    else:
        print ("You win")

TakeTurn相应地返回多个值,例如:

elif choice == 3:
    print ("You have found two shields!")
    time.sleep(1)
    shield1 = shield1 + 2
    return shield1, word1, False

你可以通过制作choices字典列表并从中随机挑选来使事情变得更整洁:

choices = [{"texts": [("You have found two shields!", 1)], 
            "shield change": 2, "return": False}, 
           {"texts": [("You have encountered an ogre...", 1.5), 
                      ("He bashes you over the head with a steel bar", 2),
                      ("You lose two shields", 1)],
            "shield change": -2, "return": False},
           ...]

然后,您的主要部分,而不是所有elif,变得简单:

choice = random.choice(choices)
for text, sleep_time in choice['texts']:
    print(text)
    time.sleep(sleep_time)
shield1 += choice['shield change']
return shield1, word1, choice['return']