You are on page 1of 9

INTRODUCTION TO

COMPUTING SCIENCE AND PROGRAMMING


Lecture 3.2: Iteration & Branching - Continue

CMPT 120, Spring 2023, Mohammad Tayebi


Study Number: 30001144

PARTICIPANTS NEEDED!
Assessing Computational Thinking in an Intro CS Course

1. Pre-assessment - Week #1
2. Post-assessment - Week #14
3. (if interested, optional) follow-up Interview ($25 gift card)
Interested? Check Canvas announcements for more information!

Questions? Contact Parsa Rajabi @ parsa_r@sfu.ca


CMPT 120, Spring 2023, Mohammad Tayebi
2
Class Agenda

• Today
• Last Time
• In-class Exercises
• Piazza Challenges
• Iteration
• Branching
• Software Engineering

• Reading
• cspy ch. 4 & 7

CMPT 120, Spring 2023, Mohammad Tayebi 3


In-class Exercise 1
• Write a program to take three numbers from user and
print the greatest one.

# this program find the largest number among


three numbers provided by user.
max = 0
for i in range(3):
x = int(input())
if x > max:
max = x
print(max, "is the greatest number.")

CMPT 120, Spring 2023, Mohammad Tayebi 4


In-class Exercise 2
• Write a program that takes two float numbers from user and
writes True if both are between 0 and 1 and False otherwise.

# this program detects whether two numbers


provided by user are between 0 and 1 or not.
x = float(input())
y = float(input())
if 0 < x < 1 and 0 < y < 1:
print("True")
else:
print("False")

CMPT 120, Spring 2023, Mohammad Tayebi 5


In-class Exercise 3
• Write a program to generate the following structure.

@
@@
@@@
@@@@ for i in range(7):
@@@@@ print("@" * (i + 1))
@@@@@@
@@@@@@@

CMPT 120, Spring 2023, Mohammad Tayebi 6


In-class Exercise 4
• Write a program to generate the following structure.

@@@@@@@
@@@@@@
@@@@@
@@@@ for i in range(7):
@@@ print("@" * (7 - i))
@@
@

CMPT 120, Spring 2023, Mohammad Tayebi 7


In-class Exercise 5
• Write a program to generate the following structure.
The optional string end is set to the
empty string to suppress newline.
print("Hello there!", end = "")
1
12
123 for i in range(7):
1234 for j in range(1, i + 2):
12345
123456 print(j, end="")
1234567 print()

Adding newline

CMPT 120, Spring 2023, Mohammad Tayebi 8


Next Lecture

Modules & Functions


Pre-reading: cspy ch. 5 & 6

CMPT 120, Spring 2023, Mohammad Tayebi


9

You might also like