简体   繁体   中英

Expected an indentation block Python error

I've looked at the similar questions but all were either a description without indentation after an if statement, or a weird mixture of spaces and tabs. I have tried removing all of the tabs and using 4 spaces and also tried with all tabs, I'm just stuck now. I've tried retyping the whole thing but I must be missing something. Any help would be greatly appreciated

EDIT: Posting the whole thing as people are getting confused about the functions which I didn't post

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()

Please also look at elif block, where you suppose to do.

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

Corrected code:

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"

See Vaibhav Mule's answer. You were using the assignment operator on input 3 and 4, rather then the comparison operator. There might be more wrong here, but it's hard to tell without the rest of your code. Also I'm not sure what your dungeon() function does, but you probably meant print?

The error was pointing to line 20 , which in this case would be line 1.

The error was in the function exit_beach() that was being called and not actually within this function.

As soon as I added the correct indentation to function exit_beach() the error disappeared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM