Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Students/Hui Zhang/session03/lab61.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
List Lab# 1
When the script is run, it should accomplish the following four series of actions:

Create a list that contains “Apples”, “Pears”, “Oranges” and “Peaches”.
Display the list.
Ask the user for another fruit and add it to the end of the list.
Display the list.
Ask the user for a number and display the number back to the user and the fruit corresponding to that number (on a 1-is-first basis).
Add another fruit to the beginning of the list using “+” and display the list.
Add another fruit to the beginning of the list using insert() and display the list.
Display all the fruits that begin with “P”, using a for loop.
"""

fruit1 = ['Apples', 'Pears', 'Oranges', 'Peaches']
print fruit1
str1 = raw_input("Please input a new fruit name here: ")
fruit1.append(str1)
print fruit1
inp2 = int(raw_input("Please input a number and i can show you the corresponding fruit: "))
print "For number: " + str(inp2) +",the corresponding fruit is: " + fruit1[inp2-1]
fruit2 = ['Banana'] + fruit1
print fruit2
fruit2.insert(0, 'Mongo')
print fruit2
for i in range(len(fruit2)):
a1 = fruit2[i]
if a1[0] == 'P':
print a1
21 changes: 21 additions & 0 deletions Students/Hui Zhang/session03/lab62.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
List Lab# 2
Using the list created in previous List Lab# 1:

Display the list.
Remove the last fruit from the list.
Display the list.
Ask the user for a fruit to delete and find it and delete it.
(Bonus: Multiply the list times two. Keep asking until a match is found. Once found, delete all occurrences.)

"""
fruit2 = ['Apples', 'Pears', 'Oranges', 'Peaches']
print fruit2.pop()
fruit3 = 2*fruit2
print fruit3
str2 = raw_input("Please input a fruit name here to be removed: ")
fruit4 = []
for i in range(len(fruit3)):

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.

you should be able to do this without keeping the index:
for f in fruit3:
if fruit3 != str2:
append(f)

a bit cleaner....

if fruit3[i] != str2:
fruit4.append(fruit3[i])
print fruit4
25 changes: 25 additions & 0 deletions Students/Hui Zhang/session03/lab63.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
List Lab# 3
using the list from series 1:

Ask the user for input displaying a line like “Do you like apples?”
for each fruit in the list (making the fruit all lowercase).
For each “no”, delete that fruit from the list.
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):
Display the list.
"""

fruit1 = ['Apples', 'Pears', 'Oranges', 'Peaches', 'Mongo', 'Banana']
fruit5 = []
for i in range(len(fruit1)):

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.

again, beter to loop directly through the items, rather than using the indices:

for f in fruit1:
etc....

input1 = raw_input("Do you like:" + fruit1[i].lower() + " (yes/no) ?")
while (input1 != 'yes') and (input1 != 'no'):
print "please answer yes or no"
input1 = raw_input("Do you like:" + fruit1[i].lower() + " (yes/no) ?")
if input1 == 'yes':
fruit5.append(fruit1[i])
# if input1 == 'no':
# continue
for i in range(len(fruit5)):
fruit5[i] = fruit5[i].lower()
print fruit5
16 changes: 16 additions & 0 deletions Students/Hui Zhang/session03/lab64.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
List Lab# 4
Once more, using the list from series 1:

Make a copy of the list and reverse the letters in each fruit in the copy.
Delete the last item of the original list. Display the original list and the copy.
"""

fruit1 = ['Apples', 'Pears', 'Oranges', 'Peaches', 'Mongo', 'Banana']
fruit6 = fruit1
fruit7 = []
for i in range(len(fruit6)):
fruit7.append(fruit6[i][::-1])
fruit1.pop()
print fruit1
print fruit7