Can anyone help me loop the rain-thunder.mp3 sound file with the playsound module in the background of my text-based game? - text-based

Whenever I hit run the sound doesn't loop but plays in its entirety before the actual text-based game runs.
Here's my code:
from playsound import playsound
def loadMap():
# These are the long descriptions for the floor plan, they tell the player where they are currently & choices they have to make.
R1ld = "I am in the Parlor, how did I end up in this place? " \
"Available directions are east."
R2ld = "R2 - I am currently in the Grand Library, it seems these books haven't been touched in ages. " \
"Available directions are north or south."
R3ld = "R3 - I made it to the study. There is old paper everywhere. " \
"Available directions are west or north."
R4ld = "R4 -Ah, the old servants quarters. There is an odd looking pedestal in the far corner of the room. " \
"Available directions are east or west"
R5ld = "R5 - I am in the kitchen, it reeks of food, a century past its expiration date. " \
"Available directions are west or north"
R6ld = "R6 - This is just a sitting room, There is an odd contraption on the wall. " \
"Available directions are west or south."
S7ld = "R7 - You are now on the second story. " \
"Available directions are west or north"
H8ld = "Proceed down the hallway or go down a level? " \
"Available directions are north or south."
R9ld = "You proceeded down the hall to the bathroom. " \
"Available directions are east or south." \
R10ld = "Turn around and go into the hall? " \
"Available directions are east or west."
R11ld = "You went back into the hall and entered the Porcelain Doll room. The dolls won't stop blinking. " \
"Available directions are east or west."
R12ld = "You exited the Porcelain Doll room and entered the Funeral Parlor/Dining Room. " \
"Available directions are east or west."
R13ld = "You left the Funeral Parlor and proceeded to the Laundry Room. " \
"Available directions are south or west."
R14ld = "You entered the Storage Room. " \
"Available directions are south or north." \
E15ld = "Exit the Storage Room? " \
"Available directions are east or west."
R16ld = "You exited the Storage Room. " \
"Available directions are south or north."
S17ld = "Go upstairs to the Third Floor? " \
"Available directions are south or east."
H18ld = "You went upstairs, you now find yourself on the Third Floor. " \
"Available directions are north or west."
R19ld = "Proceed down the hallway? " \
"Available directions are south and west."
R20ld = "You proceeded down the hall, the stench of mildew is overwhelming. " \
"Available directions are north or west."
R21ld = "You decided to enter the first bedroom, It reeks of rot and mildew. " \
"Your available directions are south and east."
R22ld = "You exited the first bedroom and entered the second bedroom. " \
"You wrote down a weird combination etched into the plaster of the wall. " \
"Right 1 time (dial 1), Left 3 times (dial 2), Left 2 times (dial 3), Right 4 times (Dial 4). " \
"(Hint: Try this combination at the pedestal in the Servants Quarters.) " \
"Available directions are south or north."
E23ld = "Exit the Room? " \
"Available directions are north or west."
H24ld = "You decided to exit the room and headed back into the hallway. " \
"Available directions are south or east."
R25ld = "Enter the Guest Bedroom? " \
"Available directions are south or east"
R26ld = "You decided the enter the Guest Bedroom, nothing to see here. " \
"Just old dusty furniture, not worth sitting on. " \
"Available directions are south or north."
R27ld = "You left the guest bedroom and proceeded down the hall to the Painting Gallery. " \
"Available directions are south or east"
R28ld = "You left the Painting Gallery and entered an Empty Bedroom. " \
" The room is empty, Aside from a set of numbers. " \
"The numbers represent the moonrise time and the time of sunset: 11:27, 6:12. " \
"These numbers could be a clue to the weird lock contraption in the sitting room on the main floor. " \
"You can go either north or east."
S29ld = "Go upstairs? " \
"Available directions are south or west."
H30ld = "You are now in the 4th floor hallway. You can go north or west." \
R31ld = "You left the hallway and entered the first room you could open. " \
"You entered the Sitting Room, Maybe you should take a rest. " \
"You can go either east or west."
R32ld = "You left the confines of the Sitting Room and entered the Smoking Room. " \
" It smells like old cigars." \
"You can go south or east"
R33ld = "You left the Smoking Room and entered the Game Room" \
" You can go either south or west."
R34ld = "You left the Game Room and opened the Master Bedroom. " \
"Your available directions are south or east. "
R35ld = "You left the Master Bedroom and entered the Balcony. The moon is full. " \
"Do you Want to go back to the Parlor? " \
"Your available directions are south or east."
R36ld = "You left the Balcony and went back to the Parlor. " \
"Available directions are north or west."
# Manor Floor plan
manorMap = {"R1": {"LocDesc": R1ld, "e": "R2", "w": "No", "n": "No", "s": "No", "lock": "No", "count": 0,
"handleSound": "rain-thunder.mp3"},
"R2": {"LocDesc": R2ld, "e": "No", "w": "No", "n": "R1", "s": "R3", "puzzle": "No", "count": 0,
"lock": "No"},
"R3": {"LocDesc": R3ld, "e": "No", "w": "R2", "n": "R4", "s": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R4": {"LocDesc": R4ld, "e": "R5", "w": "R3", "n": "No", "s": "No", "puzzle": "Rewire", "lock": "No",
"count": 0},
"R5": {"LocDesc": R5ld, "e": "No", "w": "R4", "n": "R6", "s": "No", "puzzle": "No",
"lock": "No",
"count": 0},
"R6": {"LocDesc": R6ld, "e": "No", "w": "R5", "n": "No", "s": "R7", "Lock": "The Sun & The Moon",
"Puzzle": "No", "count": 0},
"R7": {"LocDesc": S7ld, "e": "No", "w": "R6", "n": "R8", "s": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R8": {"LocDesc": H8ld, "e": "No", "w": "No", "n": "R9", "s": "R7", "puzzle": "No", "lock": "No",
"count": 0},
"R9": {"LocDesc": R9ld, "e": "R10", "w": "No", "s": "R8", "n": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R10": {"LocDesc": R10ld, "e": "R11", "w": "R9", "s": "No", "n": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R11": {"LocDesc": R11ld, "e": "R12", "w": "R10", "s": "No", "n": "No", "lock": "No", "puzzle": "No",
"count": 0},
"R12": {"LocDesc": R12ld, "e": "R13", "w": "R11", "s": "No", "n": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R13": {"LocDesc": R13ld, "s": "R12", "n": "No", "e": "No", "w": "R14", "puzzle": "No", "lock": "No",
"count": 0},
"R14": {"LocDesc": R14ld, "s": "R15", "n": "R13", "e": "No", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R15": {"LocDesc": E15ld, "s": "No", "n": "No", "e": "R14", "w": "R16", "puzzle": "No", "lock": "No",
"count": 0},
"R16": {"LocDesc": R16ld, "s": "R15", "n": "R17", "e": "No", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R17": {"LocDesc": S17ld, "s": "R16", "n": "No", "e": "R18", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R18": {"LocDesc": H18ld, "s": "No", "n": "R17", "e": "No", "w": "R19", "puzzle": "No", "lock": "No",
"count": 0},
"R19": {"LocDesc": R19ld, "s": "R20", "n": "No", "e": "No", "w": "R18", "puzzle": "No", "lock": "No",
"count": 0},
"R20": {"LocDesc": R20ld, "s": "No", "n": "R19", "e": "No", "w": "R21", "puzzle": "No", "lock": "No",
"count": 0},
"R21": {"LocDesc": R21ld, "s": "R22", "n": "No", "e": "R20", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R22": {"LocDesc": R22ld, "s": "R23", "n": "R21", "e": "No", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"E23": {"LocDesc": E23ld, "s": "No", "n": "R22", "e": "No", "w": "R24", "puzzle": "No", "lock": "No",
"count": 0},
"R24": {"LocDesc": H24ld, "s": "R25", "n": "No", "e": "R23", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R25": {"LocDesc": R25ld, "s": "R24", "n": "No", "e": "R26", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R26": {"LocDesc": R26ld, "s": "R27", "n": "R25", "e": "No", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R27": {"LocDesc": R27ld, "s": "R28", "n": "No", "e": "No", "w": "R26", "puzzle": "No", "lock": "No",
"count": 0},
"R28": {"LocDesc": R28ld, "s": "No", "n": "R27", "e": "R29", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R29": {"LocDesc": S29ld, "s": "R28", "n": "No", "e": "No", "w": "R30", "puzzle": "No", "lock": "No",
"count": 0},
"R30": {"LocDesc": H30ld, "s": "No", "n": "R31", "e": "No", "w": "R29", "puzzle": "No", "lock": "No",
"count": 0},
"R31": {"LocDesc": R31ld, "s": "No", "n": "No", "e": "R32", "w": "R30", "puzzle": "No", "lock": "No",
"count": 0},
"R32": {"LocDesc": R32ld, "s": "R33", "n": "No", "e": "R31", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R33": {"LocDesc": R33ld, "s": "R34", "n": "No", "e": "No", "w": "R32", "puzzle": "No", "lock": "No",
"count": 0},
"R34": {"LocDesc": R34ld, "s": "R35", "n": "No", "e": "R33", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R35": {"LocDesc": R35ld, "s": "R36", "n": "No", "e": "R34", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R36": {"LocDesc": R36ld, "s": "No", "n": "R1", "e": "No", "w": "R35", "puzzle": "No", "lock": "No",
"count": 0}}
return manorMap
def checkDirection(direction, currLock):
LowerD = direction.casefold()
newSpot = currLock.get(LowerD)
# print(newSpot)
if newSpot != "No":
return newSpot
else:
return None
maplist = loadMap()
startLoc = "R1"
handleSound = playsound('C:\\Users\\Student\\Documents\\Colin Lewis - Programs\\Text Based Game (Rubric)\\EscapeTheManor\\rain-thunder.mp3')
prePlayerLoc = startLoc
playerLoc = startLoc
keepPlaying = True
while keepPlaying:
currLoc = maplist.get(playerLoc)
lockName = currLoc.get("lock")
puzzleName = currLoc.get("puzzle")
currLoc["count"] += 1
print(f"Lock : {lockName} and Puzzle : {puzzleName}")
print(currLoc.get("LocDesc"))
prompt = "\n" + "Lost Soul" + ", What will be your next action? : " + ", Make your choice wisely: "
playerAction = input(prompt)
prevPlayerLoc = currLoc
if playerAction.lower() == "q":
keepPlaying = False
else:
newLoc = checkDirection(playerAction.lower(), currLoc)
if newLoc is not None:
playerLoc = newLoc
else:
print("This action cannot be completed! Carry on!")
# Puzzle locations are R4 & R6
# Functions below should handle/deal with the lock and puzzle.
#Function has been block commented out for now.
The handleSound function is what I am using for the sound, I basically want it to loop in the background and not play the whole sound file before the actual game is displayed in the console.
Any help with fixing this is much appreciated.

Related

TypeError: write() argument must be str, not int - error continues to appear

I am in the process of making a text-based game where the player has to escape an old manor. It seems that every time the program gets to the starting room "R1" in this case, I get an error.
Here is the code:
import winsound
import random
import sys
import time
# Slowtype function
def HandleSounds(SoundToPlay, kplaying):
if SoundToPlay == 'No':
return
else:
sound2Play = f"./sounds/{SoundToPlay}"
if kplaying:
winsound.PlaySound(sound2Play, winsound.SND_ALIAS | winsound.SND_ASYNC)
else:
winsound.PlaySound(sound2Play, winsound.SND_ALIAS | winsound.SND_ASYNC)
def slow_type(t, speed):
HandleSounds("Typing.wav", True)
for l in t:
sys.stdout.write(l)
sys.stdout.flush()
time.sleep(.1)
time.sleep(random.random() * 10.0 / speed)
winsound.PlaySound(None, winsound.SND_ALIAS)
print('')
# loadMap contains the long descriptions which tell the player where they are and what directions/ actions they have. It also contains the actual map.
# The sound file being played using the playsound module seems to play first before the program runs, just throwing that out there.
def loadMap():
slow_type("Welcome poor lost soul.", mediumText)
slow_type("It seems you have been trapped on the grounds of an old estate which has been left to rot and decay within the sands of time.", mediumText)
slow_type("You have 13 hours to escape, unlock the doors, solve the puzzles and find your way out.", mediumText)
slow_type("Escape or die trying, the choice is yours.", mediumText)
slow_type("May the gods be with you...", mediumText)
R1ld = ("I am in the Parlor, how did I end up in this place? ", mediumText)
("Available directions are east.", mediumText)
R2ld = ("R2 - I am currently in the Grand Library, it seems these books haven't been touched in ages. ", mediumText)
("Available directions are north or south.", mediumText)
R3ld = ("R3 - I made it to the study. There is old paper everywhere. ", mediumText)
("Available directions are west or north.", mediumText)
R4ld = ("R4 -Ah, the old servants quarters. There is an odd looking pedestal in the far corner of the room. ", mediumText)
("Move the dials or leave the room: ", mediumText)
("Available directions are east or west", mediumText)
R5ld = ("R5 - I am in the kitchen, it reeks of food, a century past its expiration date. ", mediumText)
("Available directions are west or north", mediumText)
R6ld = ("R6 - This is just a sitting room, There is an odd contraption on the wall. ", mediumText)
("Available directions are west or south. ", mediumText)
("Enter the time of moonrise and sunset: ", mediumText)
S7ld = ("R7 - You are now on the second story. ", mediumText)
("Available directions are west or north", mediumText)
H8ld = ("Proceed down the hallway or go down a level? ", mediumText)
("Available directions are north or south.", mediumText)
R9ld = ("You proceeded down the hall to the bathroom. ", mediumText)
("Available directions are east or south.", mediumText)
R10ld = ("Turn around and go into the hall? ", mediumText)
("There's a code written on the wall, it reads as follows: 1874 ", mediumText)
("Available directions are east or west.", mediumText)
R11ld = ("You went back into the hall and entered the Porcelain Doll room. The dolls won't stop blinking. ", mediumText)
("Available directions are east or west.", mediumText)
R12ld = ("You exited the Porcelain Doll room and entered the Funeral Parlor/Dining Room. ", mediumText)
("Available directions are east or west.", mediumText)
R13ld = ("You left the Funeral Parlor and proceeded to the Laundry Room. ", mediumText)
("Available directions are south or west.", mediumText)
R14ld = ("You entered the Storage Room. ", mediumText)
("Available directions are south or north.", mediumText)
E15ld = ("Exit the Storage Room? ", mediumText)
("Available directions are east or west.", mediumText)
R16ld = ("You exited the Storage Room. ", mediumText)
("Available directions are south or north.", mediumText)
S17ld = ("Go upstairs to the Third Floor? ", mediumText)
("Available directions are south or east.", mediumText)
H18ld = ("You went upstairs, you now find yourself on the Third Floor. ", mediumText)
("Available directions are north or west.", mediumText)
R19ld = ("Proceed down the hallway? ", mediumText)
("Available directions are south and west.", mediumText)
R20ld = ("You proceeded down the hall, the stench of mildew is overwhelming. ", mediumText)
("Available directions are north or west.", mediumText)
R21ld = ("You decided to enter the first bedroom, It reeks of rot and mildew. ", mediumText)
("Your available directions are south and east.", mediumText)
R22ld = ("You exited the first bedroom and entered the second bedroom. ", mediumText)
("You wrote down a weird combination etched into the plaster of the wall. ", mediumText)
("Right 1 time (dial 1), Left 3 times (dial 2), Left 2 times (dial 3), Right 4 times (Dial 4). ", mediumText)
("(Hint: Try this combination at the pedestal in the Servants Quarters.) ", mediumText)
("Available directions are south or north.", mediumText)
E23ld = ("Exit the Room? ", mediumText)
("Available directions are north or west.", mediumText)
H24ld = ("You decided to exit the room and headed back into the hallway. ", mediumText)
("Available directions are south or east.", mediumText)
R25ld = ("Enter the Guest Bedroom? ", mediumText)
("Available directions are south or east", mediumText)
R26ld = ("You decided the enter the Guest Bedroom, nothing to see here. ", mediumText)
("Just old dusty furniture, not worth sitting on. ", mediumText)
("Available directions are south or north.", mediumText)
R27ld = ("You left the guest bedroom and proceeded down the hall to the Painting Gallery. ", mediumText)
("Available directions are south or east", mediumText)
R28ld = ("You left the Painting Gallery and entered an Empty Bedroom. ", mediumText)
(" The room is empty, Aside from a set of numbers. ", mediumText)
("The numbers represent the moonrise time and the time of sunset: 11:27, 6:12. ", mediumText)
("These numbers could be a clue to the weird lock contraption in the sitting room on the main floor. ", mediumText)
("You can go either north or east.", mediumText)
S29ld = ("Go upstairs? ", mediumText)
("Available directions are south or west.", mediumText)
H30ld = ("You are now in the 4th floor hallway. You can go north or west.", mediumText)
R31ld = ("You left the hallway and entered the first room you could open. ", mediumText)
("You entered the Sitting Room, Maybe you should take a rest. ", mediumText)
("You can go either east or west.", mediumText)
R32ld = ("You left the confines of the Sitting Room and entered the Smoking Room. ", mediumText)
("It smells like old cigars.", mediumText)
("You can go south or east", mediumText)
R33ld = ("You left the Smoking Room and entered the Game Room", mediumText)
(" You can go either south or west.", mediumText)
R34ld = ("You left the Game Room and opened the Master Bedroom. ", mediumText)
("There is another weird puzzle", mediumText)
("Your available directions are south or east. ", mediumText)
R35ld = ("You left the Master Bedroom and entered the Balcony. The moon is full. ", mediumText)
("Directions are south & east.", mediumText)
R36ld = ("You left the Balcony and went back to the Parlor. ", mediumText)
("You can now leave the manor, proceed? ", mediumText)
("Please enter the code to escape: ", mediumText)
# Manor Floor plan
manorMap = {"R1": {"LocDesc": R1ld, "e": "R2", "w": "No", "n": "No", "s": "No", "lock": "No", "count": 0},
"R2": {"LocDesc": R2ld, "e": "No", "w": "No", "n": "R1", "s": "R3", "puzzle": "No", "count": 0, "lock": "No"},
"R3": {"LocDesc": R3ld, "e": "No", "w": "R2", "n": "R4", "s": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R4": {"LocDesc": R4ld, "e": "R5", "w": "R3", "n": "No", "s": "No", "puzzle": "Rewire",
"solution": "Rewire", "lock": "No",
"count": 0},
"R5": {"LocDesc": R5ld, "e": "No", "w": "R4", "n": "R6", "s": "No", "puzzle": "No",
"lock": "No",
"count": 0},
"R6": {"LocDesc": R6ld, "e": "No", "w": "R5", "n": "No", "s": "R7", "Lock": "The Sun & The Moon",
"Puzzle": "No", "count": 0},
"R7": {"LocDesc": S7ld, "e": "No", "w": "R6", "n": "R8", "s": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R8": {"LocDesc": H8ld, "e": "No", "w": "No", "n": "R9", "s": "R7", "puzzle": "No", "lock": "No",
"count": 0},
"R9": {"LocDesc": R9ld, "e": "R10", "w": "No", "s": "R8", "n": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R10": {"LocDesc": R10ld, "e": "R11", "w": "R9", "s": "No", "n": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R11": {"LocDesc": R11ld, "e": "R12", "w": "R10", "s": "No", "n": "No", "lock": "No", "puzzle": "No",
"count": 0},
"R12": {"LocDesc": R12ld, "e": "R13", "w": "R11", "s": "No", "n": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R13": {"LocDesc": R13ld, "s": "R12", "n": "No", "e": "No", "w": "R14", "puzzle": "No", "lock": "No",
"count": 0},
"R14": {"LocDesc": R14ld, "s": "R15", "n": "R13", "e": "No", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R15": {"LocDesc": E15ld, "s": "No", "n": "No", "e": "R14", "w": "R16", "puzzle": "No", "lock": "No",
"count": 0},
"R16": {"LocDesc": R16ld, "s": "R15", "n": "R17", "e": "No", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R17": {"LocDesc": S17ld, "s": "R16", "n": "No", "e": "R18", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R18": {"LocDesc": H18ld, "s": "No", "n": "R17", "e": "No", "w": "R19", "puzzle": "No", "lock": "No",
"count": 0},
"R19": {"LocDesc": R19ld, "s": "R20", "n": "No", "e": "No", "w": "R18", "puzzle": "No", "lock": "No",
"count": 0},
"R20": {"LocDesc": R20ld, "s": "No", "n": "R19", "e": "No", "w": "R21", "puzzle": "No", "lock": "No",
"count": 0},
"R21": {"LocDesc": R21ld, "s": "R22", "n": "No", "e": "R20", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R22": {"LocDesc": R22ld, "s": "R23", "n": "R21", "e": "No", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"E23": {"LocDesc": E23ld, "s": "No", "n": "R22", "e": "No", "w": "R24", "puzzle": "No", "lock": "No",
"count": 0},
"R24": {"LocDesc": H24ld, "s": "R25", "n": "No", "e": "R23", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R25": {"LocDesc": R25ld, "s": "R24", "n": "No", "e": "R26", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R26": {"LocDesc": R26ld, "s": "R27", "n": "R25", "e": "No", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R27": {"LocDesc": R27ld, "s": "R28", "n": "No", "e": "No", "w": "R26", "puzzle": "No", "lock": "No",
"count": 0},
"R28": {"LocDesc": R28ld, "s": "No", "n": "R27", "e": "R29", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R29": {"LocDesc": S29ld, "s": "R28", "n": "No", "e": "No", "w": "R30", "puzzle": "No", "lock": "No",
"count": 0},
"R30": {"LocDesc": H30ld, "s": "No", "n": "R31", "e": "No", "w": "R29", "puzzle": "No", "lock": "No",
"count": 0},
"R31": {"LocDesc": R31ld, "s": "No", "n": "No", "e": "R32", "w": "R30", "puzzle": "No", "lock": "No",
"count": 0},
"R32": {"LocDesc": R32ld, "s": "R33", "n": "No", "e": "R31", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R33": {"LocDesc": R33ld, "s": "R34", "n": "No", "e": "No", "w": "R32", "puzzle": "No", "lock": "No",
"count": 0},
"R34": {"LocDesc": R34ld, "s": "R35", "n": "No", "e": "R33", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R35": {"LocDesc": R35ld, "s": "R36", "n": "No", "e": "R34", "w": "No", "puzzle": "No", "lock": "No",
"count": 0},
"R36": {"LocDesc": R36ld, "s": "No", "n": "R1", "e": "No", "w": "R35", "puzzle": "No", "lock": "No",
"count": 0}}
return manorMap
def checkDirection(direction, currLock):
LowerD = direction.casefold()
newSpot = currLock.get(LowerD)
# print(newSpot)
if newSpot != "No":
return newSpot
else:
return None
slowText = 60
mediumText = 200
fastText = 300
superSlowText = 25
maplist = loadMap()
startLoc = "R1"
prePlayerLoc = startLoc
playerLoc = startLoc
keepPlaying = True
'''def HandleLock1():
if solvePuzzle == "R36" and input(prompt).isnumeric() == "1874":
if input(prompt) == "1874":
print("You Escaped, congratulations! (type q to exit.)")
if solvePuzzle == "R6" and input(prompt).isnumeric() == "11:27, 6:12":
if input(prompt) == "11:27" "6:12":
print("password accepted")'''
'''def HandlePuzzle2():
if solvePuzzle == "R34" and input(prompt).islower() == "eternity":
if input(prompt) == "eternity":
print("Password excepted.")
if solvePuzzle == "R4" and input(prompt).isupper() == "Right , Left , Left, Right":
if input(prompt) == "Right , Left , Left, Right":
print("Puzzle Complete!")'''
while keepPlaying:
currLoc = maplist.get(playerLoc)
lockName = currLoc.get("lock")
puzzleName = currLoc.get("puzzle")
currLoc["count"] += 1
print(f"Lock : {lockName} and Puzzle : {puzzleName}")
'''HandleSounds("rain_thunder.mp3", True)'''
slow_type(currLoc.get('LocDesc'), mediumText)
prompt = "\n" + "Lost Soul" + ", What will be your next action? : " + ", Make your choice wisely: "
playerAction = input(prompt)
prevPlayerLoc = currLoc
if playerAction.lower() == "q":
keepPlaying = False
else:
newLoc = checkDirection(playerAction.lower(), currLoc)
if playerLoc is not None:
playerLoc = newLoc
else:
print("This action cannot be completed! Carry on!")
The error is display as follows:
Traceback (most recent call last):
File "D:/Colin Lewis/Manor_Escape/ManorEscape.py", line 410, in <module>
slow_type(currLoc.get('LocDesc'), mediumText)
File "D:/Colin Lewis/Manor_Escape/ManorEscape.py", line 34, in slow_type
sys.stdout.write(l)
TypeError: write() argument must be str, not int
How can I fix this error?

Why am I getting this invalid syntax in Dictionary using Python3?

Just started Python yesterday (after learning Javascript) and really liking the language. Just ran into this syntax error today and it does not make any sense to me. I have included a space before all the semi-colons when defining the variable and did not work. Wasn't able to find a solution after searching on web either. Any help as to why I am experiencing this error is appreciated. Thanks
Here is my function
def weekday_name(day_of_week):
"""Return name of weekday.
>>> weekday_name(1)
'Sunday'
>>> weekday_name(7)
'Saturday'
For days not between 1 and 7, return None
>>> weekday_name(9)
>>> weekday_name(0)
"""
weekdays = {
"1": "Sunday"
"2": "Monday"
"3": "Tuesday"
"4": "Wedensday"
"5": "Thursday"
"6": "Friday"
"7": "Saturday"
}
return weekdays['day_of_week']
weekdays = {
"1": "Sunday"
"2": "Monday"
"3": "Tuesday"
"4": "Wedensday"
"5": "Thursday"
"6": "Friday"
"7": "Saturday"
}
Is invalid syntax. You need commas between the entries, as in
weekdays = {
"1": "Sunday",
"2": "Monday",
"3": "Tuesday",
"4": "Wedensday", # Wednesday?
"5": "Thursday",
"6": "Friday",
"7": "Saturday",
}
As an aside, you've also basically defined a list as a dictionary. OK, the numbers are strings, but why? Consider a list:
def weekday_name(day_of_week):
"""Return name of weekday.
>>> weekday_name(1)
'Sunday'
>>> weekday_name(7)
'Saturday'
For days not between 1 and 7, return None
>>> weekday_name(9)
>>> weekday_name(0)
"""
weekdays = ["Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"]
return weekdays[(int(day_of_week) - 1) % 7]

Dictionary For Loop for Scoring Word

I am creating a program that scores a scrabble game. For the part that scores the word, it uses a dictionary with the Scrabble letter values:
letter_val = {" ": 0, "a": "1", "b": 3, "c": 3, "d": 2, "e": 1, "f": 4, "g": 2, "h": 4, "i": 1, "j": 8, "k": 5, "l": 1, "m": 3, "n": 1, "o": 1, "p": 3, "q": 10, "r": 1, "s": 1, "t": 1, "u": 1, "v": 4, "w": 4, "x": 8, "y": 4, "z": 10}
It takes a word as input and loops through it with the dictionary, assigning the sum to a value. I tried something along the lines of:
value = sum(letter_val[i.lower() for i in word])
or just:
value = letter_val[i.lower() for i in word
Will I have to make a whole for loop or is there anything I'm missing in the way I'm doing it?
When you iterate through a string/list inline, it returns an array.
>>> x = "many"
>>> y = [i for i in x]
>>> y
['m', 'a', 'n', 'y']
While looking up a value in a dictionary, it's done just like index based accessing on a list.
>>> a = {"x": 1, "y": 2, "z": 3}
>>> b = a["x"]
>>> b
1
In your case, you're putting together both of the above.
So, first off, make a list from the scores equivalent to the letters in the word
>>> letter_val = {" ": 0, "a": 1, "b": 3, "c": 3, "d": 2, "e": 1, "f": 4, "g": 2, "h": 4, "i": 1, "j": 8, "k": 5, "l": 1, "m": 3, "n": 1, "o": 1, "p": 3, "q": 10, "r": 1, "s": 1, "t": 1, "u": 1, "v": 4, "w": 4, "x": 8, "y": 4, "z": 10}
>>> word = "abc"
>>> scores = [letter_val[i] for i in word]
>>> scores
[1, 3, 3]
Then find the sum of values of the list
>>> value = sum(scores)
>>> value
7

How to use objective function with MultiOutputRegressor in regression problem

I am working on a time series regression problem, predict future 5 days stock price, I think this is multiple continuous outputs (multi regression).
Hence, I use MultiOutputRegressor in sklearn to do what I want.
But the predicted value for the first three days of the future is more important than the fourth and fifth days of the future.
Therefore, I want to use a weight to penalize the first three days, anyone knows how to fix this problem with customer objective function? or have other methods to fix this problem
Data and code is below
{"date": {"0": "2003-06-30", "1": "2003-07-01", "2": "2003-07-02", "3": "2003-07-03", "4": "2003-07-04", "5": "2003-07-07", "6": "2003-07-08", "7": "2003-07-09", "8": "2003-07-10", "9": "2003-07-11"}, "open": {"0": 37.1, "1": 37.09, "2": 38.17, "3": 40.6, "4": 39.1, "5": 39.6, "6": 42.0, "7": 41.3, "8": 41.2, "9": 39.6}, "max": {"0": 37.4, "1": 38.1, "2": 38.82, "3": 40.6, "4": 39.26, "5": 41.0, "6": 42.0, "7": 41.3, "8": 41.2, "9": 39.97}, "min": {"0": 36.92, "1": 37.09, "2": 38.1, "3": 38.81, "4": 38.75, "5": 39.6, "6": 40.7, "7": 40.81, "8": 40.05, "9": 39.3}, "close": {"0": 37.08, "1": 38.05, "2": 38.69, "3": 39.0, "4": 39.26, "5": 41.0, "6": 41.19, "7": 41.22, "8": 40.05, "9": 39.91}, "stock_id": {"0": 50, "1": 50, "2": 50, "3": 50, "4": 50, "5": 50, "6": 50, "7": 50, "8": 50, "9": 50}, "target_next_1_day": {"0": 38.05, "1": 38.69, "2": 39.0, "3": 39.26, "4": 41.0, "5": 41.19, "6": 41.22, "7": 40.05, "8": 39.91, "9": 40.66}, "target_next_2_day": {"0": 38.69, "1": 39.0, "2": 39.26, "3": 41.0, "4": 41.19, "5": 41.22, "6": 40.05, "7": 39.91, "8": 40.66, "9": 40.19}, "target_next_3_day": {"0": 39.0, "1": 39.26, "2": 41.0, "3": 41.19, "4": 41.22, "5": 40.05, "6": 39.91, "7": 40.66, "8": 40.19, "9": 40.85}, "target_next_4_day": {"0": 39.26, "1": 41.0, "2": 41.19, "3": 41.22, "4": 40.05, "5": 39.91, "6": 40.66, "7": 40.19, "8": 40.85, "9": 39.8}, "target_next_5_day": {"0": 41.0, "1": 41.19, "2": 41.22, "3": 40.05, "4": 39.91, "5": 40.66, "6": 40.19, "7": 40.85, "8": 39.8, "9": 39.92}}
data = pd.DataFrame(data)
X = data[['open', 'max', 'min', 'close']]
Y = data[['target_next_1_day', 'target_next_2_day', 'target_next_3_day', 'target_next_4_day', 'target_next_5_day']]
def customer_obj(target, predict):
gradient = ????
hess = ????
return gradient, hess
clfpre = xgb.XGBRegressor(n_estimators=5, objective =customer_obj)
clf = MultiOutputRegressor(clfpre).fit(X.values, Y.values)
MultiOutputRegressor just builds independent model for each target variable. Hence creating a customized objective function cannot be useful for MultiOutputRegressor.
Your customer_obj can only account for a individual target variable. For example, you can change the default objective reg:squarederror to something like Mean Absolute Error.
From Documentation:
Multi target regression
This strategy consists of fitting one regressor per target. This is a
simple strategy for extending regressors that do not natively support
multi-target regression.

Python "'builtin_function_or_method' object has no attribute '__getitem__'" error

I have written a python code below which defines a function which returns the scrabble score of the string given to it:
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def scrabble_score(word):
word1 = (word).lower
list_a = []
list_b = range(len(word))
for i in list_b:
list_a[i] = word1[i]
total = 0
for i in list_a:
score = int(score[i])
total += score
return str(total)
What's wrong in the code above? The error I get is:
'builtin_function_or_method' object has no attribute 'getitem'
__getitem__ is called when you use [] to access an element.
When you do word1 = (word).lower, you are assigning word1 to the function lower. Rather, you should be calling the function (i.e. word.lower()).
When Python sees word1[i], it tries to access the element at the i'th index, but since word1 is a function instead of a string, Python gets confused.
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def scrabble_score (word):
word=word.lower()
total=0
for char in word:
if char in score:
total =total+ score[char]
return total
print (scrabble_score('xenophobia') )

Resources