Repository navigation
session 3 for review - #75
Conversation
| """Print each item in seq in a numbered list | ||
| """ | ||
| i = 0 # item number in displaying fruit list | ||
| for x in seq: |
There was a problem hiding this comment.
this would be better with enumerate:
for i, x in enumerate(seq):
...
PythonCHB
left a comment
There was a problem hiding this comment.
Some of tehlogic is a bit tortured -- but it looks like it all works -- nice job!
| counter = 0 | ||
| for y in fruits: | ||
| if fruits[counter] == x: | ||
| fruits.remove(x) |
There was a problem hiding this comment.
careful here -- removing from a list while you iterate over it can be error-prone!
| # For any answer that is not “yes” or “no”, prompt the user to answer | ||
| # with one of those two values (a while loop is good here) | ||
| else: | ||
| while True: |
There was a problem hiding this comment.
this wan't supposed to be in its own loop, but rather, part of the loop above :-)
| print("copy_fruits before reversing") | ||
| print(copy_fruits) | ||
| i = 0 | ||
| for x in copy_fruits: |
There was a problem hiding this comment.
if you want to do it this way, use enumerate:
for i, x in enumerate(copy_fruits):
....
but maybe better to build a new list with the reversed items:
reversed = []
for fruit in fruits:
reverses.append(fruit[::-1])
That copies and reverses in one step...
| # Separate the individual donors and donations into separate lists | ||
| i = 0 | ||
| while i < len(donors_and_donations): | ||
| donor_name.append(donors_and_donations[i]) |
There was a problem hiding this comment.
if donor_name is a list of names, better to give it a plural name -- maybe 'names'?
| i = 0 | ||
| while i < len(donors_and_donations): | ||
| donor_name.append(donors_and_donations[i]) | ||
| list_of_donations.append(donors_and_donations[i + 1]) |
There was a problem hiding this comment.
isn't this getting awkward? better to have them grouped in the first place:
donors_and_donations = [ ('Steve Jobs', [5, 10, 15]),
('Bill Gates', [34, 54]),
...
you can nest tuples and lists arbitrarily deep...
| elif answer.lower() == 'quit': | ||
| break | ||
| # If donor is in the list, add the donation | ||
| else: |
There was a problem hiding this comment.
this is a bit too much logic to have in the if block -- maybe put it in a separate function?
| """ Return a copy of a sequence with the elements reversed | ||
| (just with slicing) | ||
| """ | ||
| new_seq = seq[-1:0:-1] + seq[0:1] |
There was a problem hiding this comment.
that's a bit more complicated than it needs to be. try [::-1] :-)
| then last third, then the first third in the new order | ||
| """ | ||
| # slice index of last first_third_seq element | ||
| one_third_len = len( |
|
|
||
|
|
||
| def format_tuple(seq): | ||
| print('file_{:0>3d} : {:.2f}, {:.2e}, {:.2e}'.format( |
There was a problem hiding this comment.
I was hoping you'd find the "g" formatter here -- but this works fine.
| form_string = form_string + '{} ' | ||
| temp = form_string.split() | ||
| s = ',' | ||
| form_string = s.join(temp) |
There was a problem hiding this comment.
better to just build it up in a list in the first place, rather than splitting so you can rejoin:
but you got the key idea of dynamically creating a format string -- nice!
No description provided.