CS 110
Input, multiplication, and math
Benjamin Dicken
Announcements
Read → Prep Problem → Video / (optional) live→ Repeat
The Goal: House printing
What size house should be printed? 3
___^___
/ \
| H |
|___H___|
What size house should be printed? 12
____________^____________
/ \
| H |
|____________H____________|
What size house should be printed? 7
_______^_______
/ \
| H |
|_______H_______|
What size house should be printed? 0
^
/ \
|H|
|H|
Comments
# This is come code that will print out two lines of text
print('He said, "What is up?"')
print("Joe's friend didn't reply.")
Comments
#
# Author: Benjamin Dicken
# Class: CSc 110
# Description: A program that . . .
#
Integers and Variables
Integers and Variables
age = 32
years_in_service = 17
wing_width = 25
What will this print out?
name = 'Joe'
age = 35
inches = 72
print('Hello', name)
print('you are', age, 'years old')
print('and', inches, 'inches tall')
Activity
Multiplying a string
Multiplying a string
name = 'CSc' * 3
print(name)
Multiplying a string
name = 'CSc' * 3
print(name)
CScCScCSc
Multiplying a string
name = 'CSc' * 10
print(name)
CScCScCScCScCScCScCScCScCScCSc
String Multiplication question
What will this print out? Don’t use your computer, use the whiteboard!
print('#' * 2)
print('#' * 4)
print('#' * 6)
print('#' * 8)
Activity
String Multiplication question
What will this print out. Use your white board - no computers!
print(' ' * 5, 'A' * 1)
print(' ' * 4, 'B' * 3)
print(' ' * 3, 'C' * 5)
print(' ' * 2, 'D' * 7)
print(' ' * 1, 'E' * 9)
print(' ' * 0, 'F' * 11)
Activity
Revisiting House printing
What size house should be printed? 3
___^___
/ \
| H |
|___H___|
What size house should be printed? 12
____________^____________
/ \
| H |
|____________H____________|
What size house should be printed? 7
_______^_______
/ \
| H |
|_______H_______|
What size house should be printed? 0
^
/ \
|H|
|H|
Revisiting House printing
What size house should be printed? 3
___^___
/ \
| H |
|___H___|
What size house should be printed? 12
____________^____________
/ \
| H |
|____________H____________|
What size house should be printed? 7
_______^_______
/ \
| H |
|_______H_______|
What size house should be printed? 0
^
/ \
|H|
|H|
Let’s write it!
Step 1
Here's a size 3 house:
___^___
/ \
| H |
|___H___|
Activity
Step 1
1 print("Here's a size 3 house:")
2 print(" ___^___")
3 print("/ \ ")
4 print("| H |")
5 print("|___H___|")
Step 2
What size house should be printed? X
___^___
/ \
| H |
|___H___|
1 print("Here's a size 3 house:")
2 print(" ___^___")
3 print("/ \ ")
4 print("| H |")
5 print("|___H___|")
Activity
Step 2
1 size = input('What size house should be printed? ')
2 print(" ___^___")
3 print("/ \ ")
4 print("| H |")
5 print("|___H___|")
Step 3
What size house should be printed? 3
___^___
/ \
| H |
|___H___|
What size house should be printed? 12
____________^____________
/ \
| H |
|____________H____________|
What size house should be printed? 7
_______^_______
/ \
| H |
|_______H_______|
What size house should be printed? 0
^
/ \
|H|
|H|
Activity
Step 3
What size house should be printed? 3
___^___
/ \
| H |
|___H___|
What size house should be printed? 12
____________^____________
/ \
| H |
|____________H____________|
What size house should be printed? 7
_______^_______
/ \
| H |
|_______H_______|
What size house should be printed? 0
^
/ \
|H|
|H|
Did you convert the input value to an integer?
Converting an input value to a string
Does not work:
width = input('enter width: ')
print('-' * width)
Converting an input value to a string
Does not work:
width = input('enter width: ')
print('-' * width)
Does work:
width = int(input('enter width: '))
print('-' * width)
What about the space?
What about the space?
What about the space?
print('_' * 5, '^', '_' * 5)
_____ ^ _____
print('_' * 5 + '^' + '_' * 5)
_____^_____
Step 3
What size house should be printed? 3
___^___
/ \
| H |
|___H___|
What size house should be printed? 12
____________^____________
/ \
| H |
|____________H____________|
What size house should be printed? 7
_______^_______
/ \
| H |
|_______H_______|
What size house should be printed? 0
^
/ \
|H|
|H|
Activity
house.py
size = int(input('What size house should be printed? '))
print(' ' + '_' * size + '^' + '_' * size + ' ')
print('/' + ' ' * size + ' ' + ' ' * size + '\\')
print('|' + ' ' * size + 'H' + ' ' * size + '|')
print('|' + '_' * size + 'H' + '_' * size + '|')
Newlines
Newlines
What does it print?
a = int(input('input a: ')) # 5
b = int(input('input b: ')) # 2
o = '#' * a + '\n'
t = '|' * a + '\n'
print(o * b + t * b)
Activity
What does this print?
a = int(input('input a: ')) # 10
b = int(input('input b: ')) # 1
o = '#' * a + '\n'
r = o * 2 + '\n\n'
print(o + r + o)
Activity
Vertical Difference
Smiles
The Goal: Face printing
What size face should be printed? 0
///|\\\
/ 0 0 \
| I |
| \___/ |
\_____/
The Goal: Face printing
What size face should be printed? 1
///|\\\
/ 0 0 \
| |
| I |
| |
| \___/ |
\_____/
The Goal: Face printing
What size face should be printed? 3
///|\\\
/ 0 0 \
| |
| |
| |
| I |
| |
| |
| |
| \___/ |
\_____/
Implement the face program!
Activity
Face Printing Step 1
A face:
///|\\\
/ 0 0 \
| I |
| \___/ |
\_____/
Activity
Face Printing Step 2
What size face should be printed? 3
///|\\\
/ 0 0 \
| I |
| \___/ |
\_____/
Activity
Face Printing Step 3
What size face should be printed? 3
///|\\\
/ 0 0 \
| |
| |
| |
| I |
| |
| |
| |
| \___/ |
\_____/
What size face should be printed? 1
///|\\\
/ 0 0 \
| |
| I |
| |
| \___/ |
\_____/
What size face should be printed? 0
///|\\\
/ 0 0 \
| I |
| \___/ |
\_____/
Activity
height = int(input('input: '))
print(' ///|\\\\\\')
print('/ 0 0 \\')
print('| |\n' * height, end="")
print('| I |')
print('| |\n' * height, end="")
print('| \___/ |')
print(' \_____/')
Division in Python
age = 35
half_age = age / 2
print('You' * half_age)
Activity
Two numeric types: float and integer
age = 35 # integer
score = 81.23 # float
height_meters = 1.8288 # float (6 feet)
print(type(age))
print(type(score))
print(type(height_meters))
Division in Python
age = 35
half_age = age / 2
Division in Python
a = 52 / 7
e = 27 / 5
c = int(a)
print(e, '#' * c, c)
print('#' * c, e)
print(e, a, e)
Activity
5.4 ####### 7
####### 5.4
5.4 7.428571428571429 5.4
Flag
Enter flag size: 10
#####-----
#####-----
----------
----------
Enter flag size: 20
##########----------
##########----------
--------------------
--------------------
Enter flag size: 30
###############---------------
###############---------------
------------------------------
------------------------------
Activity
flag
width = int(input('Enter flag size: '))
half_width = int(width / 2)
print()
print('#' * half_width + '-' * half_width)
print('#' * half_width + '-' * half_width)
print('-' * width)
print('-' * width)
Flag (version B)
Enter flag width: 10
Enter flag height: 4
#####-----
#####-----
----------
----------
Enter flag width: 20
Enter flag height: 6
##########----------
##########----------
##########----------
--------------------
--------------------
--------------------
Enter flag width: 30
Enter flag height: 8
###############---------------
###############---------------
###############---------------
###############---------------
------------------------------
------------------------------
------------------------------
------------------------------
Activity
Flag (version B)
width = int(input('Enter flag width: '))
height = int(input('Enter flag height: '))
half_width = int(width / 2)
half_height = int(height / 2)
print()
upper_row = '#' * half_width + '-' * half_width + '\n'
lower_row = '-' * width + '\n'
print(upper_row * half_height, end='')
print(lower_row * half_height)