我该如何解决这些Python问题?

-1 投票
1 回答
41 浏览
提问于 2025-04-14 16:05

我正在做一个项目,起初进展得很顺利,直到我遇到了一些具体的问题。现在我已经写完了代码,但却出现了33个以上的问题!以下是我的代码:

health = 100
location = "Borobudur Temple"

print(
    "As a traveler, you will visit places in Java in order to find the Java Crown."
)
print("You will solve puzzles and go through a series of challenges.")
print("You appear to be heading to the Borobudur Temple.")
print("Lets start with your name. What is it?")

name = input("> ")
print("Hello, " + name + "!")

print("Ready to begin?")
ready = input("> ")

print("Wonderful! Lets begin this journey.")

print(
    "\nAs you walk along the trees around the Borobudur Temple, you see a large bush which is blocking the way."
)
print("Would you like to walk through it, or find another way?")

pathChoice = input("> ").lower()

if pathChoice == "walk through it":
    health -= 5
    print("Health lost: -5, current health:" + str(health) + ".")
    print("Looks like you have accidentally gotten cuts from the thorns. Be careful!\n")
    print("You have now reached the Borobudur Temple, do you wish to enter it?")
    enterChoice = input("> ").lower()

    if enterChoice == "yes":
        print("You have entered the Borobudur Temple. You see a large door with a keyhole. Do you wish to try and open it?")
        openChoice = input("> ").lower()

        if openChoice == "yes":
            print("It seems to be unsuccessful. You need to find another way to open it.")
            print("You have to find a key!")
            print("You notice two paths, one leading left and the other right. Which path do you choose?")
            keyPath = input("> ").lower()

            if keyPath == "left":
                # Puzzles to get key when going left
                print("You have chosen to go left. A genie appears in front of you.")
                print("The genie presents you with a riddle: 'I speak without a mouth and hear without ears. I have no body, but I come alive with the wind. What am I?'")

                answer = input("> ").lower()

                if answer == "echo":
                    print("Congratulations! You have solved the riddle and obtained the key.")

            elif keyPath == "right":
                # Puzzles to get the key when going right
                print("You have chosen to go right. A maze appears in front of you. Navigate through the maze to get the key!")
            else:
                print("Invalid choice for the key path.")

            print("You have obtained the key. Would you like to explore and open the large door?")
            exploreChoice = input("> ").lower()

            if exploreChoice == "yes":
                # Code for exploring further
                print("You decide to explore further within the temple.")
    # Additional exploration code can be added here

    # Maze navigation
    print("After walking through dark corridors and solving intricate puzzles, you find yourself in front of a maze.")
    print("You must navigate through the maze to reach the other side.")
    maze = [
        "##########",
        "#    #   #",
        "###  #   #",
        "#    #####",
        "###      #",
        "##########"
    ]

    def print_maze(maze):
        for row in maze:
            print(row)

    print_maze(maze)

    print("Navigate through the maze using 'w' to move up, 'a' to move left, 's' to move down, and 'd' to move right.")

    current_position = [1, 1]
    max_attempts = 10
    attempts = 0

    while maze[current_position[0]][current_position[1]] != "#":
        move = input("Enter your move (w/a/s/d): ").lower()

        if move == "w":
            if maze[current_position[0]] - 1:
        elif move == "a":
            if maze[current_position[0]][current_position[1] - 1] != "#":
                current_position[1] -= 1
        elif move == "s":
            if maze[current_position[0] + 1][current_position[1]] != "#":
                current_position[0] += 1
        elif move == "d":
            if maze[current_position[0]][current_position[1] + 1] != "#":
                current_position[1] += 1

        print_maze(maze)

    print("Congratulations! You have navigated through the maze and reached the other side.")
    # Final puzzle and Java Crown
    print("You have now entered a hidden secret room within the temple. Among a few statues, there is a mysterious box hanging from above.")
    print("You need to solve the final puzzle to reveal the contents of the box and claim the Java Crown.")

    final_puzzle = input("Final puzzle: What is always in front of you but can't be seen? ")

    if final_puzzle.lower() == "future":
        print("Congratulations! You have solved the final puzzle.")
        print("As you solve the puzzle, the box opens and from within it descends the Java Crown!")
        print("Thank you for playing. You have successfully found the Java Crown and completed the adventure.")
    else:
        print("Incorrect answer. The box remains closed. You need to solve the final puzzle to reveal the Java Crown.")
else:
    # Code for opening the door
    print("You have chosen to open the large door.")
    print("As you open the door, you discover a maze-like passage ahead.")
    print("You must now find your way through the maze to progress further.")
    # Additional code for maze navigation can be added here as shown above
    # End of the game for this path
    print("Thank you for playing. The adventure ends here.")

elif keyPath.lower() == "right":
  # Puzzles to get the key when going right
  print("You have chosen to go right. A maze appears in front of you. Navigate through the maze to get the key!")
else:
  print("Invalid choice for the key path.")

#KEJJJ
else:
print("Incorrect answer. The genie disappears, and you must find another way to get the key.")

    elif enterChoice.lower() == "no":
        print("You have decided to not enter the Borobudur Temple.")
    else:
        print("You have chosen to not enter the Borobudur Temple.")

# Find another way
elif pathChoice.lower() == "find another way":
    print("Wonderful choice. You begin walking towards the temple.\n")
    print("You have now reached the Borobudur Temple, do you wish to enter it?")
    enterChoice = input("> ")

                # enter
                if enterChoice.lower() == "yes":
                    print("You have entered the Borobudur Temple. You see a large door with a keyhole. Do you wish to try and open it?")
                    openChoice = input("> ")
                    # yes
                    if openChoice.lower() == "yes":
                        print("It seems to be unsuccessful. You need to find another way to open it.")
                        print("You have to find a key!")
                        print("You notice two paths, one leading left and the other right. Which path do you choose?")
                        keyPath = input("> ")

                        if keyPath.lower() == "left":
                            # Puzzles to get key when going left
                            print("You have chosen to go left. A genie appears in front of you.")
                            print("The genie presents you with a riddle: 'I speak without a mouth and hear without ears. I have no body, but I come alive with the wind. What am I?'")

                            answer = input("> ")
                            if answer.lower() == "echo":
                                print("Congratulations! You have solved the riddle and obtained the key.")

                        elif keyPath.lower() == "right":
                            # Puzzles to get the key when going right
                            print("You have chosen to go right. A maze appears in front of you. Navigate through the maze to get the key!")
                        else:
                            print("Invalid choice for the key path.")

                        print("You have obtained the key. Would you like to explore and open the large door?")
                        exploreChoice = input("> ")
                        if exploreChoice.lower() == "yes":
                            # Code for exploring further
                            print("You decide to explore further within the temple.")
                # Additional exploration code can be added here
                # Maze navigation
                print("After walking through dark corridors and solving intricate puzzles, you find yourself in front of a maze.")
                print("You must navigate through the maze to reach the other side.")
                maze = [
                    "##########",
                    "#    #   #",
                    "###  #   #",
                    "#    #####",
                    "###      #",
                    "##########"
                ]
                def print_maze(maze):
                    for row in maze:
                        print(row)

                print_maze(maze)

                print("Navigate through the maze using 'w' to move up, 'a' to move left, 's' to move down, and 'd' to move right.")

                current_position = [1, 1]
                while maze[current_position[0]][current_position[1]] != "#":
                    move = input("Enter your move (w/a/s/d): ")

                    if move == "w":
                        if maze[current_position[0] - 1][current_position[1]] != "#":
                            current_position[0] -= 1
                    elif move == "a":
                        if maze[current_position[0]][current_position[1] - 1] != "#":
                            current_position[1] -= 1
                    elif move == "s":
                        if maze[current_position[0] + 1][current_position[1]] != "#":
                            current_position[0] += 1
                    elif move == "d":
                        if maze[current_position[0]][current_position[1] + 1] != "#":
                            current_position[1] += 1

                    print_maze(maze)

                print("Congratulations! You have navigated through the maze and reached the other side.")
                # Final puzzle and Java Crown
                print("You have now entered a hidden secret room within the temple. Among a few statues, there is a mysterious box hanging from above.")
                print("You need to solve the final puzzle to reveal the contents of the box and claim the Java Crown.")

                final_puzzle = input("Final puzzle: What is always in front of you but can't be seen? ")

                if final_puzzle.lower() == "future":
                    print("Congratulations! You have solved the final puzzle.")
                    print("As you solve the puzzle, the box opens and from within it descends the Java Crown!")
                    print("Thank you for playing. You have successfully found the Java Crown and completed the adventure.")
                else:
                    print("Incorrect answer. The box remains closed. You need to solve the final puzzle to reveal the Java Crown.")
              else:
                # Code for opening the door
                print("You have chosen to open the large door.")
                print("As you open the door, you discover a maze-like passage ahead.")
                print("You must now find your way through the maze to progress further.")
                # Additional code for maze navigation can be added here as shown above
                # End of the game for this path
                print("Thank you for playing. The adventure ends here.")
            elif keyPath.lower() == "right":
                # Puzzles to get the key when going right
                print("You have chosen to go right. A maze appears in front of you. Navigate through the maze to get the key!")
              else:
                print("Invalid choice for the key path.")

    else:
        print("You have chosen to not enter the temple.")
else:
    print("I'm sorry, but that is an Invalid choice. Please try again!")

这是我遇到的问题:

图片-问题

主要是,正如我之前提到的,我正在做一个Python项目。我需要今天完成这个项目,之前我快完成了,但突然出现了这些错误。我尝试修改了一些内容,以及问题标签中提到的内容。

1 个回答

2

你的代码出现了缩进错误和控制流相关的错误。幸运的是,这两种问题都很容易解决。

缩进错误


在Python中,缩进错误可能会以几种不同的方式出现。比如,当程序期待看到一个缩进的代码块,但实际上没有找到时,就会出错。

举个例子,在大约第95行,你写了:

...

        if move == "w":
            if maze[current_position[0]] - 1: #Python expected a block after this line!
        elif move == "a":
            if maze[current_position[0]][current_position[1] - 1] != "#":
                current_position[1] -= 1
...

这里的if语句旁边有个注释,但里面什么都没有,所以Python就报错了。

另外,你在缩进时使用了4个空格和2个空格交替,这样会让代码看起来很混乱。为了让代码更易读,最好保持一种缩进风格。虽然有些风格指南推荐用2个空格,但通常大家都习惯用4个空格。

类似的问题出现在第145行,那里一个if语句突然缩进了3个制表符,后面的行又缩回来了。记得在需要缩进的地方统一使用4个空格。

控制流错误


关于elif的问题,你在代码中多次在else后面使用elif。简单来说,else语句应该放在控制流的最后,而elif必须跟在if后面。想了解更多,可以查看官方文档

举个例子,最外层的结构是这样的:

if ...   #Line 25- Starting with an if is correct!
    #code
else ... #Line 121- #else has to be last
    #code
elif ... #Line 130- #elif has to be before the else
    #code
else ... #Line 133- #else has to be last
    #code
else ... #Line 137- #else has to be last
    #code
elif ... #Line 146-  #elif has to be before the else
    #code
else ... #Line 250- Ending with an else is correct!
    #code      

在第25行有一个if,然后在第121行有一个else,接着在第130行有一个elif,第133行又有一个else,第137行又有一个else(还有一个缺失的缩进),第146行有一个elif,最后在第250行又有一个else

修正控制流的关键词和缩进就能解决你的错误。

撰写回答