簡體   English   中英

預期的縮進塊Python錯誤

[英]Expected an indentation block Python error

我已經看過類似的問題,但是所有問題都是在if語句后沒有縮進的描述,或者是空格和制表符的奇怪混合。 我嘗試刪除所有選項卡並使用4個空格,並且還嘗試了所有選項卡,但現在我被困住了。 我已經嘗試過重新輸入整個內容,但是我一定會丟失一些內容。 任何幫助將不勝感激

編輯:隨着人們對我沒有發布的功能感到困惑,發布了整個內容

from sys import exit

class player:
    str = 0
    wep_dam = 0
    dam = str + wep_dam
    cha = 0
    sne = 0
    arm = 0
    max_life = 10
    points_remaining = 0
    cave_save = False
    cave_fork_save = False
    dragon_save = False


def exit_beach():
    print "Walking further up the beach from water, you see a cave."
    print "Above the cave hangs a warning sign, it reads:\n"
    print "\"DANGER: Tresspassers will be killed and eaten\""
    ans = raw_input("Ignore the warning and enter the cave?\n\n1. Enter the cave\n2. Continue walking\n\n> ")
    if ans == "1":
        cave()
    elif ans == "2":
        troll()
    else:
        print "Error, you didn't enter 1 or 2\n"





def shadow_figure():
    print "\n\nYou approach the figure, who remains silent."
    print "As you get closer you realise he has bag at his feet."
    print "Mysterious figure: \"You may choose only one.\""
    print "You look into the bag, and see a shiny sword on top of a large steel shield."
    ans = raw_input("Do you: \n1. Take the sword \n2. Take the shield \n3. Take the whole bag and run \n4. Walk away without taking anything\n> ")
    if ans == "1":
        print "The sword gives you an extra 3 damage"
        player.wep_dam += 3
        exit_beach()

    elif ans == "2":
        print "The shield gives you 3 armor, but it's so heavy it reduces your sneak by 1"
        player.arm += 3
        player.sne -= 1
        exit_beach()

    elif ans == "3":
        dungeon("You get about 10 feet away with the bag before bony fingers grip your throat and choke you unconscious")

    elif ans == "4":
        exit_beach()
    else:
        print "Error, please enter anumber between 1 and 4"

def beach():
    print "\n\nYou wake up on a beach with no idea how you got there. \nYou see a shadowy figure close to the water."
    ans = raw_input("Do you: \n1. Approach him \n2. Go the other way\n> ")
    if ans == "1":
        shadow_figure()
    elif ans == "2":
        exit_beach()
    else:
        print "Please enter either 1 or 2"

def dungeon(why):
    print why
    if not player.cave_save and not player.dragon_save and not player.cave_fork_save:
        print "Unfortunately you didn't get far enough to continue from a saved point, \n would you like to restart from the beginning? (Yes/No)"
        ans = raw_input("> ")
        ans = ans.lower()
        if ans == "yes":
            reset_stats()
            start()
        else:
            end()
    elif player.cave_save and not player.dragon_save and not player.cave_fork_save:
        print "Would you like to continue from the cave entrance or start over?"
        print "1. Cave entrance\n2. Start over\n3. Exit game"
        ans = raw_input("> ")
        if ans == "1":
            cave()
        elif ans == "2":
            reset_stats()
            start()
        else:
            end()
    elif player.cave_save and player.cave_fork_save and not player.dragon_save:
        print "Would you like to continue from the cave entrance, the cave fork, or start over?"
        print "1. Cave entrance\n2. Cave fork\n3. Start over\n4. Exit game"
        ans = raw_input("> ")
        if ans == "1":
            cave()
        elif ans == "2":
            cave_fork()
        elif ans == "2":
            reset_stats()
            start()
        else:
            end()
    else:
        print "Havent done this part yet"



def reset_stats():
    str = 0
    wep_dam = 0
    dam = str + wep_dam
    cha = 0
    sne = 0
    arm = 0

    max_life = 10

    points_remaining = 10
    print "\n\n\n\nGame Reset\n\n\n\n"

def end():
    print "Thank you for playing"
    exit(0)

def start():


    print "You are an adventurer, your stats are currently:"
    print "Strength:  %d \nCharisma:  %d \n  Sneak:   %d" % ( player.str,  player.cha,  player.sne)
    print "Strength determines your damage, charisma determines your chance of pursuasion, \nand sneak determines whether or not you can go get past enemies without being detected"
    print "you have 10 points available to spend, to spend a point, simply type the number which corresponds\nwith the skill and hit enter"
    print "\n\n1. Strength \t2. Charisma \t3. Sneak\n"
    player.points_remaining = 10
    while player.points_remaining > 0:
        ans = raw_input("Choose a skill: ")
        if ans == "1":
            player.str += 1
            player.points_remaining -= 1
            print "Strength is now  %d" % ( player.str)
            print "%d  points remaining\n" % ( player.points_remaining)

        elif ans == "2":
            player.cha += 1
            player.points_remaining -= 1
            print "Charisma is now %d" % ( player.cha)
            print "%d points remaining\n" % ( player.points_remaining)

        elif ans == "3":
            player.sne += 1
            player.points_remaining -= 1
            print "Sneak is now %d" % ( player.sne)
            print "%d points remaining\n" % (player.points_remaining)
        else:
            print "Error, please enter a number from 1 to 3\n"

    print "Your stats are now: "
    print "Strength:  %d \nCharisma:  %d \n   Sneak:  %d\n\n" % ( player.str,  player.cha,  player.sne)
    print "Is this OK? Or would you like to restart?\n"
    ans = raw_input("1. Continue \n2. Restart\n> ")
    if ans == "1":
        print "Game will now begin...."
        beach()
    elif ans == "2":
        ans = raw_input("Are you sure? Yes/No\n> ")
        ans = ans.lower()
        if ans == "yes":
            reset_stats()
            start()
        else:
            beach()
    else:
        print "Error, please enter 1 or 2"


start()

還請查看您應該在哪里做的elif塊。

elif ans == "3": # make sure you have '==' operator here.
elif ans == "4":

更正的代碼:

def shadow_figure():
    print "\n\nYou approach the figure, who remains silent."
    print "As you get closer you realise he has bag at his feet."
    print "Mysterious figure: \"You may choose only one.\""
    print "You look into the bag, and see a shiny sword on top of a large steel shield."
    ans = raw_input("Do you: \n1. Take the sword \n2. Take the shield \n3. Take the whole bag and run \n4. Walk away without taking anything\n> ")

    if ans == "1":
        print "The sword gives you an extra 3 damage"
        wep_dam += 3
        exit_beach()

    elif ans == "2":
        print "The shield gives you 3 armor, but it's so heavy it reduces your sneak by 1"
        arm += 3
        sne -= 1
        exit_beach()

    elif ans == "3":
        dungeon("You get about 10 feet away with the bag before bony fingers grip your throat and choke you unconscious") #Should dungeon() be print instead?

    elif ans == "4":
        exit_beach()
    else:
        print "Error, please enter a number between 1 and 4"

請參閱Vaibhav Mule的答案。 您在輸入3和4上使用了賦值運算符,而不是比較運算符。 這里可能存在更多錯誤,但是如果沒有其余代碼,很難分辨出來。 另外我不確定您的dungeon()函數做什么,但是您可能是要打印的?

錯誤指向第20行 ,在本例中為第1行。

錯誤出在正在調用的函數exit_beach() ,而實際上不在此函數內。

一旦向函數exit_beach()添加正確的縮進,錯誤就消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM