Python无效语法出现在事先没有注册无效语法的代码上

2024-03-28 08:39:56 发布

您现在位置:Python中文网/ 问答频道 /正文

错误从“currentRoom='Entrance'行开始。然后它就 它说,下面的任何东西都是“无效语法”。如果我删除所有这些,它会说 “解析时出现意外的EOF”

另外,堆栈溢出对我的字典的代码来说很奇怪,所以很抱歉——看起来不是这样的

import time
import winsound
import pickle
bgm = "D:\The Mansion\HallofFear.wav"
winsound.PlaySound(bgm, winsound.SND_LOOP | winsound.SND_ASYNC)
deaths = 0
k = (int(deaths))

#!/bin/python3

def showInstructions():
    #print a main menu and the commands
    print('''
THE MANSION
===============
Welcome to the Mansion.

Can you find the key and escape its grasp?

COMMANDS:
go [direction]
(east, west, north, south, up, down)
get [item]
fight [enemy]
door (if there's a door)
save
load
quit
''')

def showStatus():
    #print the player's current status
    print('---------------------------')
    print('You are in the ' + currentRoom)
    #print the current inventory
    print('Inventory : ' + str(inventory))
    #print the kill count
    print('You have',k,'kills.')
    #print an item if there is one
    if "item" in rooms[currentRoom]:
        print('You see a ' + rooms[currentRoom]['item'])
    #print enemy presence if there's an enemy
    if 'enemy' in rooms[currentRoom]:
        print(rooms[currentRoom]['enemy'] + " is present!")
    if 'door' in rooms[currentRoom]:
        print('You see a door leading to the ' + rooms[currentRoom]['door'] + '.')
    print("---------------------------")


#an inventory, which is initially empty
inventory = []

#a dictionary linking a room to other rooms
rooms = {

    'Entrance' : {
        'south' : 'Hall A',
        'west' : 'Restroom A',
    },

    'Hall A' : { 
    'north' : 'Entrance',
    'south' : 'Dining Room',
    'enemy' : 'tiny slime',
    },

    'Kitchen' : {
    'north' : 'Cupboard',
    'west' : 'Dining Room',
    'item' : 'knife',
    },

    'Cupboard' : {
    'south' : 'Kitchen',
    'enemy' : 'pupi doge',
    },

    'Dining Room' : {
    'north' : 'Hall A',
    'east' : 'Kitchen',
    'enemy' : 'doge',
    'west' : 'Parlor Room',
    },

    'Restroom A' : {
        'east' : 'Entrance',
        'west' : 'Toilet',
        'item' : 'water',
    },

    'Toilet' : {
    'east' : 'Restroom A',
        'enemy' : 'tiny slime',
    },

    'Parlor Room' : {
    'west' : 'Fireplace',
    'south' : 'Stairs',
    'north' : 'Study',
    'east' : 'Dining Room',
    },

    'Fireplace' : {
    'east' : 'Parlor Room',
    'north' : 'Study',
    'enemy' : 'foxfire',
    'item' : 'torch',
    },

    'Study' : {
        'south' : 'Parlor Room',
    'item' : 'tome',
    'secret' : 'Hidden Passage A',
        'door' : 'Garden',
    },

    'Stairs' : {
    'down' : 'Parlor Room',
    'up' : 'Hall B',
    },

    'Hall B' : {
    'down' : 'Stairs',
    'east' : 'Intersecting Halls',
    'enemy' : 'pupi doge',
    'secret' : 'Hidden Passage B',
    },

    'Intersecting Halls' : {
    'west' : 'Hall B',
    'east' : 'Bedroom A',
    'door' : 'Billiards Room',
    'south' : 'Hall C',
    },

    'Hidden Passage A' : {
        'south' : 'Study',
    'item' : 'sword',
    'enemy' : 'skelly',
    'item' : 'garden key',
    'north' : 'Storage',
    },

    'Storage' : {
    'enemy' : 'doge',
    'enemy' : 'skelly',
    'item' : 'coin',
    'south' : 'Hidden Passage A',
    },

    'Hall C' : {
    'enemy' : 'ghostie',
    'south' : 'Garden',
    },

    'Bedroom A' : {
    'item' : 'lighter',
    'enemy' : 'slime',
    'enemy' : 'doge',
    'west' : 'Intersecting Halls',
    },

    'Billiards Room' : {
    'item' : 'sword',
    'enemy' : 'skelly',
        'item' : 'ladder',
    'enemy' : 'slime',
    },

    'Garden' : {
        'item' : 'billiards key',
        'enemy' : 'slime',
        'south' : 'Study',
        'north' : 'Greenhouse',
    },

    'Greenhouse' : {
        'item' : 'water',
        'item' : 'water',
        'enemy' : 'mandrake',

}

#Start the player in the Entrance
_currentRoom = 'Entrance'_

showInstructions()

#loop forever
while True:

    showStatus()

    #get the player's next 'move'
    #.split() breaks it up into an list array
    #eg typing 'go east' would give the list:
    #['go','east']
    move = ''
    while move == '':  
        move = input('>')

    move = move.lower().split()

    #if they type 'go' first
    if move[0] == 'go':
        #check that they are allowed wherever they want to go
        if move[1] in rooms[currentRoom]:
            #set the current room to the new room
            currentRoom = rooms[currentRoom][move[1]]
        #there is no door (link) to the new room
        else:
            print('You can\'t go that way!')


    #if they type 'get' first
    if move[0] == 'get' :
        #if the room contains an item, and the item is the one they want to get
        if "item" in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
            #add the item to their inventory
            inventory += [move[1]]
            #display a helpful message
            print(move[1] + ' got!')
            #delete the item from the room
            del rooms[currentRoom]['item']
            #otherwise, if the item isn't there to get
        else:
        #tell them they can't get it
            print('Can\'t get ' + move[1] + '!')

    if move[0] == 'fight' :
    #check if there's an enemy in the room.
        if 'enemy' in rooms[currentRoom] and move[1] in rooms[currentRoom]['enemy']:
            print(rooms[currentRoom]['enemy'] + ' approaches!')
            time.sleep(1)
            if rooms[currentRoom]['enemy'] == 'tiny slime' or rooms[currentRoom]['enemy'] == 'pupi doge':
                print(rooms[currentRoom]['enemy'] + ' was defeated!')
                del rooms[currentRoom]['enemy']
                k += 1
            elif rooms[currentRoom]['enemy'] == 'doge' or rooms[currentRoom]['enemy'] == 'slime' :
                if 'knife' in inventory:
                    print(rooms[currentRoom]['enemy'] + ' was defeated!')
                    del rooms[currentRoom]['enemy']
                    k += 1
                else:
                    print("You were defeated by " + rooms[currentRoom]['enemy'] + "...")
                    input("Press any key to quit.")
                    quit()
            elif rooms[currentRoom]['enemy'] == 'ghostie' or rooms[currentRoom]['enemy'] == 'skelly' :
                if 'sword' in inventory:
                    print(rooms[currentRoom]['enemy'] + ' was defeated!')
                    del rooms[currentRoom]['enemy']
                    k += 1
                elif 'sword' not in inventory:
                    print("You were defeated by " + rooms[currentRoom]['enemy'] + "...")
                    input("Press any key to quit.")
                    quit()
            elif rooms[currentRoom]['enemy'] == 'foxfire' or rooms[currentRoom]['enemy'] == 'demon' :
                if 'water' in inventory:
                    print("You doused " + rooms[currentRoom]['enemy'] + " with water!")
                    inventory -= ['water']
                    if 'knife' in inventory:
                        print(rooms[currentRoom]['enemy'] + ' was defeated!')
                        del rooms[currentRoom]['enemy']
                        k += 1
                    elif 'knife' not in inventory:
                        print("You were defeated by " + rooms[currentRoom]['enemy'] + "...")
                        input("Press any key to quit.")
                        quit()
                elif 'water' not in inventory:
                    if 'sword' in inventory:
                        print(rooms[currentRoom]['enemy'] + ' was defeated!')
                        del rooms[currentRoom]['enemy']
                        k += 1
                    elif 'sword' not in inventory:
                        print("You were burnt to a crisp by " + rooms[currentRoom]['enemy'] + "...")
                        input("Press any key to quit.")
                        quit()
            elif rooms[currentRoom]['enemy'] == 'mandrake' or rooms[currentRoom]['enemy'] == 'treant' :
                if 'torch' in inventory:
                    print("You used the torch to burn the " + rooms[currentRoom]['enemy'] + " to a crisp!")
                    inventory -= ['torch']
                    del rooms[currentRoom]['enemy']
                    k += 1
                else:
                    if rooms[currentRoom]['enemy'] == 'mandrake':
                        if 'knife' in inventory:
                            print(rooms[currentRoom]['enemy'] + ' was defeated!')
                            del rooms[currentRoom]['enemy']
                            k += 1
                        else:
                            print("You were defeated by " + rooms[currentRoom]['enemy'] + "...")
                            input("Press any key to quit.")
                            quit()
                    if

        else:
            print("There is no enemy of that name!")

    if move[0] == 'save' :
        #save room, inventory, and kill count to a DAT file
        with open('savefile.dat', 'wb') as f:
            pickle.dump([currentRoom, inventory, k], f, protocol=2)
            print("The game has been saved!")

    if move[0] == 'load' :
        #load data from DAT file and display status
        with open('savefile.dat', 'rb') as f:
            currentRoom, inventory, k = pickle.load(f)
            print("The game has loaded!")


    if move[0] == 'quit' :
        quit()


    if move[0] == 'door' :
        if rooms[currentRoom]['door'] == 'Garden':
            if 'garden key' in inventory:
                print('You used the key on the Garden door.')
                rooms[currentRoom] == 'Garden'
            else:
                print('You do not have the proper key.')
        elif rooms[currentRoom]['door'] == 'Billiards Room':
            if 'billiards key' in inventory:
                print('You used the key on the Billiards Room door.')
                rooms[currentRoom] == 'Billiards Room'
            else:
                print('You do not have the proper key.')
        elif rooms[currentRoom] == 'Forest Path E':
            if 'observatory key' in inventory:
                print('You used the key on the Observatory door.')
                rooms[currentRoom] == 'Observatory'
            else:
                print('You do not have the proper key.')

Tags: thetokeyinyoumoveifitem
3条回答

currentRoom = 'Entrance'替换_currentRoom = 'Entrance'_

是的,奥利,看来你是对的(没有结束括号)我不敢相信我没有注意到!再见,再次感谢

改变

#Start the player in the Entrance
_currentRoom = 'Entrance'_

#Start the player in the Entrance
_currentRoom = 'Entrance'

相关问题 更多 >