In: Computer Science
Exercise: Change Orb Location
-------------------------
### Description
In this series of exercises, you will create functions
to create, modify and examine dictionaries that
represent information about a memory orb. Memory
orbs have three important pieces of information:
The `emotion` that created the memory, the `location`
the memory is stored, and the `data` which is the
information in the memory.
For this exercise, you will create a function that
will change the value of the `location` key in a
dictionary.
### Files
* `orbfunctions.py` : set of functions to work with memory orbs.
### Function Name
`move_orb`
### Parameters
* `orb` : a dictionary
* `new_location` : a string, the new value for `location`
### Action
Changes the value associated with the key `location`.
### Return Value
The modified dictionary, `orb`.
def create_orb(emotion, location, data):
dict = {
"emotion":emotion,
"location":location,
"data":data
}
return dict
def orb_get_emotion(orb):
return orb["emotion"]
def change_orb_emotion(orb, new_emotion):
orb.update({"emotion":new_emotion})
return orb
def orb_get_location(orb):
if orb.get('location'):
return orb.get('location')
else:
return None
def move_orb(orb,location):
if
'''Functions to create ,modify and examine dictionaries'''
#create dictionary named orb with value and key provided
def create_orb(emotion,location,data):
orb = {
#set the values from the parameter
'emotion':emotion,
'location':location,
'data':data
}
return orb #return the dictionary orb
#Set memory location
def mov_orb(orb,new_location):
orb['location'] = new_location #To set the location
return orb #return the modified dictionary orb
#Get memory location
def orb_get_location(orb):
return orb.get('location') #Return the memory location of key location
#Set emotion
def change_orb_emotion(orb,new_emotion):
orb['emotion']=new_emotion #Set the emotion of memory
return orb #Return modified dictionary orb
#Get the emotion
def orb_get_emotion(orb):
return orb.get('emotion') #Return the emotion of memory
'''Include the data methods of set and get.Additionally'''
#Set data in memory
def change_orb_data(orb,new_data):
orb['data']=new_data #Set the value
return orb #Return the modified value
#Get the data which is the information in the memory
def orb_get_data(orb):
return orb.get('data') #return the data
'''To test the code'''
#Create an orb dictionary with random values
orb_created = create_orb("temperature low","flash","Computer memories")
print("Orb Dictionary \n ", orb_created) #Printed the Orb dictionary
modified_location_orb= mov_orb(orb_created,"RAM") #Call function mov_orb() to change location
print("Location Modified Orb \n",modified_location_orb)
newlocation = orb_get_location(modified_location_orb) #Function to return orb location
print("Newlocation\n",newlocation)
modified_emotion_orb = change_orb_emotion(modified_location_orb,"temperature rise") #Call change_orb_emotion to change emotion
print("Emotion Modified Orb \n",modified_emotion_orb)
newemotion = orb_get_emotion(modified_emotion_orb) #Function to return emotion
print("NewEmotion \n:",newemotion)
'''Included change and get data function additionally'''
modified_data_orb = change_orb_data(modified_emotion_orb,"Pictures") #Call function change_orb_data() to change data
print("Data Modified Orb\n",modified_data_orb)
newdata= orb_get_data(modified_data_orb) #Function to get orb data
print("NewData\n",newdata)
Summary:
Function to create, modidy and examine dictionary orb.
Step: Create dictionary with provided parameters emotion,location and data.
Step2: Set and Get the location in orb dictionary
1) To set the location use the subscript notation dictionary[key] = value
2) To get the location use get() method as if the key not found if return None without any errors
Step 3: Repeat the above step for both emotion and data
Step 4: The set methods return the modified dictionary orb whereas the get methods return the parameter changed.
Additonally to the asked quest i have include the change_orb_data() and orb_get_data() for your reference.