Order of execution, more strings, for-loops, range function

$24.99 $18.99

Starting Lab 4 Open a browser and log into Brightspace On the left hand side under Labs tab, find lab4 material contained in lab4-students.zip file Download that file to the Desktop and unzip it. Before starting, always make sure you are running Python 3 This slide is applicable to all labs, exercises, assignments … etc…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:

Description

5/5 – (2 votes)

Starting Lab 4

  • Open a browser and log into Brightspace

  • On the left hand side under Labs tab, find lab4 material contained in lab4-students.zip file

  • Download that file to the Desktop and unzip it.

Before starting, always make sure you are running Python 3

This slide is applicable to all labs, exercises, assignments … etc

ALWAYS MAKE SURE FIRST that you are running Python 3.4 (3.5 or 3.6 is fine too)

That is, when you click on IDLE (or start python any other way) look at the first line that the Python shell displays. It should say Python 3.4 or 3.5 or 3.6 (and then some extra digits)

If you do not know how to do this, read the material provided with Lab 1. It explains it step by step

Exercise from: Introduction to Computing Using Python by Lj. Perkovic

Introduction to Computing Using Python by Lj. Perkovic

Indexing operator, revisited

Introduction to Computing Using Python by Lj. Perkovic

Task 2

Start python interpreter

  1. In python interpreter, create aha variable and assign ‘abcdefgh’ to it.

  1. Then write Python expressions (in the interpreter) using string aha and the indexing operator that evaluate to:

    1. abcd’

    1. def’

    1. h’

    1. fg’

    1. defgh’

    1. fgh’

    1. adg’

    1. be’

Introduction to Computing Usi

Introduction to Computing Using Python by Lj. Perkovic

Task 3

Copy/paste the following expression (in black) to Python Interpreter:

s = ”’It was the best of times, it was the worst of times; it was the age of wisdom, it was the age of foolishness;

it was the epoch of belief, it was the epoch of incredulity; it was …”’

(The beginning of A Tale of Two Cities by Charles Dickens.)

Then do the following, in order:

  1. Write a sequence of statements that produce a copy of s, named newS, in which characters ., ,, ;, and \n have been replaced by blank spaces.

  1. Remove leading and trailing blank spaces in newS (and name the new string newS).

  1. Make all the characters in newS lowercase (and name the new string newS).

  1. Compute the number of occurrences in newS of string ‘it was’.

  1. Change every occurrence of was to is (and name the new string newS).

Task 4

  1. Follow the link in (b) and trace through the two Python Vizualizer examples that have “Code Lence 1” and “Code Lence 2” in the caption. You do that by clicking Forward until the program ends, like we did it class (It does not matter here that it says Python 2.7. For these two programs Python 2 and 3 behave the same).

  1. Then Answer the two multiple choice questions at the end

https://runestone.academy/runestone/static/thinkcspy/Functions/Functionsthatreturnvalues.html

c) Then follow this link and do the two multiple choice exercises:

https://runestone.academy/runestone/static/thinkcspy/Functions/FlowofExecutionSummary.html

Task 5: More tracing and print vs return

Open the program called print-vs-return-and-function-calls.py, provided in this lab.

The lines the start with # are commented out. Thus Python will ignore them. In other words, if you press Run Module the lines that start with # will not be executed. Uncomment the lines as instructed in the file. Each time you do, save and press “Run Module”. But before you press “Run Module”, write down what you think the program will print. Then press “Run Module” and compare.

Note : once you uncomment a line as instructed, leave it as is (do not put back the comments, but rather continue with the next set of lines you are instructed to uncomment).

Programming exercise: Solved Problem

Suppose that you are given the following two problems to solve.

Read the two problems. Think about how you would solve them, and then open and study the two provided solutions in prog_solved_v1.py and prog_solved_v2.py. Run both.

Version 1: Write a program that asks a user for her name and age and prints a nice message stating if the user is eligible to vote.

Version 2: Write a program that asks a user for name and age and

prints a nice message stating if the user is eligible to vote. As a part

of your solution the program should have a function called

is_eligible that given the age as input parameter returns true or

false depending on weather the age is less than 18 or not.

Programming exercise 1

Repeat the exercise in the previous question (version 2), where in addition you need to ask the user for their citizenship and if they are currently in prison convicted for a criminal offence. Your program should print a nice message telling the user if they are eligible to vote (i.e. if they are 18+, Canadian and do not live in prison convicted for a criminal offence, then they can vote. Otherwise not). You should modify function is_eligible so it takes to additional paramters as input. In particular the head of the function should be: is_eligible(age, citizenship, prison)

Your program should work if the user enters any of the following versions of answers for the two new questions:

Canadian

Canada

Canada

canadian

Yes

YES

No

no

and so on

Note that in Canada, one can vote even if in prison convicted for a criminal offence. This example if fictional.

Programming exercise 2

Write a function called mess that takes a phrase (i.e., a string) as input and then returns the copy of that phrase where each character that is one of the last 8 consonants of English alphabet is capitalized (so, r, s, t, v, w, x,y , z) and where each blank space is replaced by dash.

For this question, use a for loop over characters of a string, and “accumulator”. (We will see, or have seen, that in Lecture 8 on Monday). When called from the python shell, your function should behave as follows:

  • mess(Random access memory) Random-acceSS-memoRY–

  • mess(central processing unit.)

cenTRal-pRoceSSing—uniT.

Introduction to Computing Usi

Introduction to Computing Usi

Examples of for loops with range()

Python Visualizer

Go to Python Visualizer here (make sure you choose Python 3)

http://www.pythontutor.com/visualize.html#mode=edit

and copy/paste, only by one, the following loops to it and click Forward to visualize the execution. Pay attention what is assigned to variable i and when does loop terminate.

Programming exercise 3:

Open the file ex23n8.py. Inside of that file:

  1. write a function called print_all_23n8(num), that takes as input a non-negative integer num and prints all the the non-negative numbers smaller than num that are divisible by 2 or 3 but not 8. You should use the function that is already present in the file (and that you developed for the last lab)

  1. Outside of that function ask the user for a non-negative integer. Your program should then print all non-negative numbers that are divisible by 2 or 3 but not 8, by making a call to function print_all_23n8

  1. Run your program and test it by entering, for example, 1000 when prompted for a number

Programming exercise 4:

This program:

for i in range(4):

print(“*************”)

Prints :

*************

*************

*************

*************

Write a program that asks a user for a positive integer and a character. And then draws half piramid with the given number of raws using that character. For example if the user enter 3 and $, your program should draw (that is, print):

$

#

$$

###

$$$

#####

Bonus excercise:

#######

For a callenge, draw a real pirmaid like the one to the right

#########

that should be displayed if the user entered 10 and #

###########

#############

###############

#################

19

###################

Programming exercise 5:

  1. Write a program that asks a user for a positive integer and then prints all the divisors of the given integer. For example if the user entered 6, the program should print: 1, 2, 3, 6

  1. Add a function, called prime, to this program. Function prime takes a positive integer as input parameter and tests if it is a prime (that is, it returns true if the given number is a prime and false otherwise). Recall that a number is prime if it at least 2 and if it is only divisible by 1 and itself. Then make a call to this function and print the message stating if the number the user inputted in 1) is a prime.

  1. Copy/paste your whole solution into Python Visualizer, click through with Forward to understand see how it runs

  1. Bonus exercise: Write a program that asks a user for a positive integer, n, and prints all the primes smaller than n.

Making a table

With these tools, you now can make a program that prints nice tables. See here:

http://interactivepython.org/courselib/static/thinkcspy/MoreAboutIteration/SimpleTables.html

21

Order of execution, more strings, for-loops, range function
$24.99 $18.99