-
Notifications
You must be signed in to change notification settings - Fork 76
session03 List Lab Homework #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
357be69
Adding List Lab homework
jerrytozhang a0bcb3d
Delete lab1.py
jerrytozhang aa4ee42
Delete lab2.py
jerrytozhang 934352e
Delete lab3.py
jerrytozhang 6487a8f
Delete lab4.py
jerrytozhang d933b14
Delete lab5.py
jerrytozhang 03aa254
Delete lab63.py~
jerrytozhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)): | ||
| if fruit3[i] != str2: | ||
| fruit4.append(fruit3[i]) | ||
| print fruit4 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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....