Posts: 22
Threads: 6
Joined: Jun 2020
Hi guys,
I have half of this code running. It's the other half that's busting my brain.
On second thought, when I run it outside of the function the first half works. Inside the function...no clue because I can't get the other half running.
Once again, your support is appreciated.
buckets = [ ('[email protected]',{'first_name':'john','last_name':'doe'}),
('[email protected]',{'first_name':'jane','last_name':'doe'}),
('[email protected]',{'first_name':'derek','last_name':'zoolander'}),
('[email protected]',{'first_name':'murph','last_name':'cooper'}),
('[email protected]',{'first_name':'ned','last_name':'stark'})
]
def update_record(list_of_records, email, first_name, last_name):
"""
This function is created to reset the value of the first_name and last_name
for a record found with a specific email address while leaving the rest of
the list unchanged. If the email address does not exist, then append a new
record to the list with the new email address, first name and last name.
"""
## Write code below ##
new_values = []
for k, v in enumerate(list_of_records):
email, full_name = v
#email = v[0]
first_name = full_name['first_name']
last_name = full_name['last_name']
if 'email' == email and 'first_name' != first_name and 'last_name' != last_name:
full_name.update({'first_name': first_name})
full_name.update({'last_name': last_name})
# elif 'email' != email and 'first_name' != first_name and 'last_name' != last_name:
# #email['email'].append('email')
# full_name['first_name'].append('first_name')
# full_name['last_name'].append(last_name)
## end of function ##
update_record(buckets,'[email protected]','jon','snow')
Posts: 12,032
Threads: 486
Joined: Sep 2016
Try:
buckets = [ ('[email protected]',{'first_name':'john','last_name':'doe'}),
('[email protected]',{'first_name':'jane','last_name':'doe'}),
('[email protected]',{'first_name':'derek','last_name':'zoolander'}),
('[email protected]',{'first_name':'murph','last_name':'cooper'}),
('[email protected]',{'first_name':'ned','last_name':'stark'})
]
def update_record(list_of_records, email, first_name, last_name):
for n, entry in enumerate(list_of_records):
if entry[0] == email:
entry[1]['first_name'] = first_name
entry[1]['last_name'] = last_name
break
else:
entry = [email, {'first_name': f"{first_name}", 'last_name': f"{last_name}"}]
list_of_records.append(entry)
update_record(buckets,'[email protected]','jon','snow')
for entry in buckets:
print(entry) results:
Output: ('[email protected]', {'first_name': 'jon', 'last_name': 'snow'})
('[email protected]', {'first_name': 'jane', 'last_name': 'doe'})
('[email protected]', {'first_name': 'derek', 'last_name': 'zoolander'})
('[email protected]', {'first_name': 'murph', 'last_name': 'cooper'})
('[email protected]', {'first_name': 'ned', 'last_name': 'stark'}
Posts: 22
Threads: 6
Joined: Jun 2020
Hi Larz60,
Thank you very much but two things:
1. I don't know how but I copied and pasted code, however my output is the original list:
Output: Changing john's first name to jon and last name to snow
('[email protected]', {'first_name': 'john', 'last_name': 'doe'})
('[email protected]', {'first_name': 'jane', 'last_name': 'doe'})
('[email protected]', {'first_name': 'derek', 'last_name': 'zoolander'})
('[email protected]', {'first_name': 'murph', 'last_name': 'cooper'})
('[email protected]', {'first_name': 'ned', 'last_name': 'stark'})
2. The second half of the code was supposed compare the email, first_name and last_name and if none of the givens were in the list then that record should be appended to the list.
For ex: ' [email protected]', 'iron', 'man' (this record does not figure in the list and should be appended.)
Cheers,
(Dec-28-2021, 11:41 AM)Larz60+ Wrote: Try:
buckets = [ ('[email protected]',{'first_name':'john','last_name':'doe'}),
('[email protected]',{'first_name':'jane','last_name':'doe'}),
('[email protected]',{'first_name':'derek','last_name':'zoolander'}),
('[email protected]',{'first_name':'murph','last_name':'cooper'}),
('[email protected]',{'first_name':'ned','last_name':'stark'})
]
def update_record(list_of_records, email, first_name, last_name):
for n, entry in enumerate(list_of_records):
if entry[0] == email:
entry[1]['first_name'] = first_name
entry[1]['last_name'] = last_name
break
else:
entry = [email, {'first_name': f"{first_name}", 'last_name': f"{last_name}"}]
list_of_records.append(entry)
update_record(buckets,'[email protected]','jon','snow')
for entry in buckets:
print(entry) results:
Output: ('[email protected]', {'first_name': 'jon', 'last_name': 'snow'})
('[email protected]', {'first_name': 'jane', 'last_name': 'doe'})
('[email protected]', {'first_name': 'derek', 'last_name': 'zoolander'})
('[email protected]', {'first_name': 'murph', 'last_name': 'cooper'})
('[email protected]', {'first_name': 'ned', 'last_name': 'stark'}
Posts: 582
Threads: 1
Joined: Aug 2019
The "else" in line 14 should belong to the "for" block, not to the "if" block. So it should be un-indented.
Posts: 22
Threads: 6
Joined: Jun 2020
Nope...that doesn't work either. Returns the same output.
Quick question though...if, else and elif always go together, how can it be for the "for loop"?
(Dec-28-2021, 03:55 PM)ibreeden Wrote: The "else" in line 14 should belong to the "for" block, not to the "if" block. So it should be un-indented.
Posts: 12,032
Threads: 486
Joined: Sep 2016
Ibreden was correct,
with code as follows:
buckets = [ ('[email protected]',{'first_name':'john','last_name':'doe'}),
('[email protected]',{'first_name':'jane','last_name':'doe'}),
('[email protected]',{'first_name':'derek','last_name':'zoolander'}),
('[email protected]',{'first_name':'murph','last_name':'cooper'}),
('[email protected]',{'first_name':'ned','last_name':'stark'})
]
def update_record(list_of_records, email, first_name, last_name):
for n, entry in enumerate(list_of_records):
if entry[0] == email:
entry[1]['first_name'] = first_name
entry[1]['last_name'] = last_name
break
else:
entry = [email, {'first_name': f"{first_name}", 'last_name': f"{last_name}"}]
list_of_records.append(entry) results are:
Output: ('[email protected]', {'first_name': 'jon', 'last_name': 'snow'})
('[email protected]', {'first_name': 'jane', 'last_name': 'doe'})
('[email protected]', {'first_name': 'derek', 'last_name': 'zoolander'})
('[email protected]', {'first_name': 'murph', 'last_name': 'cooper'})
('[email protected]', {'first_name': 'ned', 'last_name': 'stark'})
If you do not get these results, you didn't copy correctly
BashBedlam likes this post
Posts: 582
Threads: 1
Joined: Aug 2019
Dec-28-2021, 06:28 PM
(This post was last modified: Dec-28-2021, 06:29 PM by ibreeden.)
(Dec-28-2021, 05:54 PM)Men Wrote: Quick question though...if, else and elif always go together, how can it be for the "for loop"? Please read " The for statement" to see how it is defined.
The "else" block will be executed once after the "for" block has successfully executed N times. (Where N >= 0 .) The "else" block will not be executed if the "for" block is ended with a "break" statement.
That is exactly what you want. The for loops through all records. If it finds the record to be updated it updates it and leaves the loop with "break" so the "else" is not executed. If the record to be updated is not found, the "else" is entered which inserts the record with the new data.
Posts: 22
Threads: 6
Joined: Jun 2020
Really!!! Is that an insult to my intelligence or what?
(Dec-28-2021, 06:16 PM)Larz60+ Wrote: If you do not get these results, you didn't copy correctly
Nevertheless, it's not running...I'll continue trying on my own.
Thx for the support. Cheers.
Posts: 22
Threads: 6
Joined: Jun 2020
Will do...I'll read up on it.
(Dec-28-2021, 06:28 PM)ibreeden Wrote: (Dec-28-2021, 05:54 PM)Men Wrote: Quick question though...if, else and elif always go together, how can it be for the "for loop"? Please read "The for statement" to see how it is defined.
The "else" block will be executed once after the "for" block has successfully executed N times. (Where N >= 0 .) The "else" block will not be executed if the "for" block is ended with a "break" statement.
That is exactly what you want. The for loops through all records. If it finds the record to be updated it updates it and leaves the loop with "break" so the "else" is not executed. If the record to be updated is not found, the "else" is entered which inserts the record with the new data.
Posts: 12,032
Threads: 486
Joined: Sep 2016
The results that are shown in my last post are actual results from running the code shown.
Nothing more nor less.
|