繁体   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