Before the Lab

Before the Lab

<p> CS177 LAB 5</p><p>Before the Lab Study Chapter 5 of the textbook.</p><p>Setting up your Environment Go to your working directory in “data.cs.purdue.edu” and create a directory “cs177/lab05”. Refer to the first lab or the other handouts on the course page if you need to remember the steps to do so. Then start the IDLE Python Interpreter.</p><p>Exercise 1 Complete the following program to display the output expected by replacing every “?” string with an appropriate string or list function (e.g. s[0]). Do not use string constants (e.g. “Hello World”).</p><p># # lab5-1.py: # Complete the following program to print the desired output. # def Main(): s = "I love Python Programming." y = " Yes!" print() print("Char at index 10 of s:", "?") print() print("Chars 2 to 5 inclusive of s: ", "?") print() print("Last character of s:", "?") print() print("Length of s:", "?") print() print("s repeated 3 times:", "?") print() print("s concatenated with y:", s + y) print() #You are not allowed to just type in the new sentence, manipulate the given string s. print("I love Programming Python: ", "?") print()</p><p> l=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]</p><p> print("Weekdays:", "?") print() print("Weekend:", "?") print() print("Two weeks:", "?") print() print("Days of the week:") for i in range(len(l)): print("?")</p><p>Main()</p><p>This is the expected output:</p><p>Char at 10 of s: h</p><p>Chars 2 to 5 inclusive of s: love</p><p>Last character of s: .</p><p>Length of s: 26 s repeated 3 times: I love Python Programming.I love Python Programming.I love Python Programming. s concatenated with y: I love Python Programming. Yes! I love Programming Python: I love Programming Python </p><p>Weekdays: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']</p><p>Weekend: ['Sunday', 'Saturday']</p><p>Two weeks: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']</p><p>Days of the week: 0 : Sunday 1 : Monday 2 : Tuesday 3 : Wednesday 4 : Thursday 5 : Friday 6 : Saturday</p><p>Save the program in cs177/lab05/mystring.py. Exercise 2</p><p>Write a program that will generate 5 random sentences. The random sentences will be built using one component from each of the following lists:</p><p> subjects + verbs + objects + endings</p><p> where:</p><p> subjects=['I','You','Bob','John','Sue','Kate', 'The lizard people'] verbs=['will search for','will get', 'will find','attained','found', 'will start interacting with', 'will accept','accepted'] objects=['Billy','an apple','a Triforce', 'the treasure','a sheet of paper'] endings=['.',', right?','.', ', like I said.','.',', just like you.']</p><p>(Lists based on the sentence generator in http://pastehtml.com/view/1c0gckz.html)</p><p>You can use the function:</p><p> from random import *</p><p> s = choice(subjects) print s</p><p> where choice will choose a random component from the sequence “subjects”.</p><p>Your program should print 5 random sentences. </p><p>Example: >>> Sue will accept a Triforce . I attained a sheet of paper , like I said. Sue accepted the treasure . John will get the treasure , just like you. Kate will get Billy , just like you. >>></p><p>Save the program in cs177/lab05/sentences.py. Exercise 3</p><p>Given the following lists of colors and fruits, write a program that generates all possible combinations. Hint: Use two nested for loops.</p><p> fruits = [ "apple", "strawberry", "banana", "pear", "watermelon", "tomato"] color = ["green", "red", "blue", "orange", "pink", "violet"]</p><p>Example output:</p><p>>>> green apple red apple blue apple orange apple pink apple violet apple green strawberry red strawberry blue strawberry orange strawberry pink strawberry violet strawberry green banana red banana blue banana …</p><p>Save the program in cs177/lab05/combinations.py.</p><p>Turnin your project Run PuTTY and login to data.cs.purdue.edu. </p><p>Turn in your lab by typing: cd cs177 turnin –v –c cs177 –p lab05 lab05</p><p>(Remember that the final “lab05” is the name of your folder.) Lab5 Grading Form</p><p>Name: ______</p><p>Login: ______</p><p>Max Current Exercise 1 30 Submission (7) -2 points for every wrong question Heading and comments (3) Exercise 2 30 Submission (7) Generate random sentence (10) Print 5 sentences (10) Heading and comments (3) Exercise 3 30 Submission (6) Use for loops (10) Print all combinations (10) Heading and comments (4) Attend your Scheduled Lab 10 Total: 100</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us