'Adding variables to dictionary, same format
I want to make the variables that a user inputs, add to the dictionary. But I want it to be added the way that it currently looks:
bookLogger = [
{'BookName': 'Noise', 'Author': 'Daniel Kahneman', 'Process': 'Reading' },
{'BookName': 'Hunting Party', 'Author': 'Lucy Foley', 'Process': 'Reading'},
{'BookName': 'Superintelligence', 'Author': 'Nick Bostrom', 'Process': 'Not Reading'}
]
The code that I have tried
def AddingBooks():
#User inputs what they want to add tot he library
BName = input("Please enter the book name: ")
BAuthor = input("Enter the books Author: ")
BProcess = input("Are you reading, not reading or haven't started ")
#Want to add this to the bookLogger but in the same format that the
#bookLogger dictionary is in
bookLogger['BookName'] = BName
bookLogger['Author'] = BAuthor
bookLogger['Process'] = BProcess
The error that comes up is list indices must be integers or slices, not str for the bookLogger['BookName'] = BName line.
NOTE:
Not sure how to change it + not sure if that will add it to the bookLogger the way I want it to.
Solution 1:[1]
bookLogger is a list that has a dictionary. So You have to push or append the dictionary to the list.
bookLogger = [
{'BookName': 'Noise', 'Author': 'Daniel Kahneman', 'Process': 'Reading' },
{'BookName': 'Hunting Party', 'Author': 'Lucy Foley', 'Process': 'Reading'},
{'BookName': 'Superintelligence', 'Author': 'Nick Bostrom', 'Process': 'Not Reading'}
]
def AddingBooks():
#User inputs what they want to add tot he library
BName = input("Please enter the book name: ")
BAuthor = input("Enter the books Author: ")
BProcess = input("Are you reading, not reading or haven't started ")
#Want to add this to the bookLogger but in the same format that the bookLogger
bookLogger.append({"BookName": BName, "Author": BAuthor, "Process": BProcess})
Solution 2:[2]
The error is due to using string as a list index. i.e bookLogger['BookName']. 'BookName' is an index. But you cannot use such string values for the list.
If your logger is
bookLogger = [
{'BookName': 'Noise', 'Author': 'Daniel Kahneman', 'Process': 'Reading' },
{'BookName': 'Hunting Party', 'Author': 'Lucy Foley', 'Process': 'Reading'},
{'BookName': 'Superintelligence', 'Author': 'Nick Bostrom', 'Process': 'Not Reading'}
]
You can write a function this way.
def AddingBooks():
# User inputs what they want to add tot he library
BName = input("Please enter the book name: ")
BAuthor = input("Enter the books Author: ")
BProcess = input("Are you reading, not reading or haven't started ")
# Want to add this to the bookLogger but in the same format that
the
# bookLogger dictionary is in
#first create a dictionary
log = {'BookName': BName,
'Author': BAuthor,
"Process": BProcess}
#bookLogger is a list so you have to append individual log to it
bookLogger.append(log)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Md Jewele Islam |
| Solution 2 |
