Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

session 3 for review - #75

Merged
PythonCHB merged 12 commits into
UWPCE-PythonCert:masterfrom
ea8606:master
Oct 28, 2017
Merged

PythonCHB merged 12 commits into
UWPCE-PythonCert:masterfrom
ea8606:master

Conversation

@ea8606

@ea8606 ea8606 commented Oct 24, 2017

Copy link
Copy Markdown
Contributor

No description provided.

@PythonCHB PythonCHB left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not finished yet

"""Print each item in seq in a numbered list
"""
i = 0 # item number in displaying fruit list
for x in seq:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be better with enumerate:

for i, x in enumerate(seq):
    ...

@PythonCHB PythonCHB left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not on one line?



def format_tuple(seq):
print('file_{:0>3d} : {:.2f}, {:.2e}, {:.2e}'.format(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@PythonCHB
PythonCHB merged commit 2d81779 into UWPCE-PythonCert:master Oct 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants