CS177 LAB 2 - Purdue University



CS177 LAB 5

Before the Lab

Study Chapter 5 of the textbook.

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.

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”).

|# |

|# 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() |

| |

|l=["Sunday", "Monday", "Tuesday", |

|"Wednesday", "Thursday", "Friday", "Saturday"] |

| |

|print("Weekdays:", "?") |

|print() |

|print("Weekend:", "?") |

|print() |

|print("Two weeks:", "?") |

|print() |

|print("Days of the week:") |

|for i in range(len(l)): |

|print("?") |

| |

|Main() |

This is the expected output:

|Char at 10 of s: h |

| |

|Chars 2 to 5 inclusive of s: love |

| |

|Last character of s: . |

| |

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

| |

|Weekdays: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] |

| |

|Weekend: ['Sunday', 'Saturday'] |

| |

|Two weeks: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', |

|'Wednesday', 'Thursday', 'Friday', 'Saturday'] |

| |

|Days of the week: |

|0 : Sunday |

|1 : Monday |

|2 : Tuesday |

|3 : Wednesday |

|4 : Thursday |

|5 : Friday |

|6 : Saturday |

Save the program in cs177/lab05/mystring.py.

Exercise 2

Write a program that will generate 5 random sentences. The random sentences will be built using one component from each of the following lists:

subjects + verbs + objects + endings

where:

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.']

(Lists based on the sentence generator in )

You can use the function:

from random import *

s = choice(subjects)

print s

where choice will choose a random component from the sequence “subjects”.

Your program should print 5 random sentences.

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. |

|>>> |

Save the program in cs177/lab05/sentences.py.

Exercise 3

Given the following lists of colors and fruits, write a program that generates all possible combinations. Hint: Use two nested for loops.

fruits = [ "apple", "strawberry", "banana", "pear", "watermelon", "tomato"]

color = ["green", "red", "blue", "orange", "pink", "violet"]

Example output:

>>>

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



Save the program in cs177/lab05/combinations.py.

Turnin your project

Run PuTTY and login to data.cs.purdue.edu.

Turn in your lab by typing:

cd cs177

turnin –v –c cs177 –p lab05 lab05

(Remember that the final “lab05” is the name of your folder.)

Lab5 Grading Form

Name: ___________________________________

Login: ___________________________________

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

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download