1 of 52

CS 110

Input, multiplication, and math

Benjamin Dicken

2 of 52

Announcements

  • Going to try breakout rooms
  • Public vs private posts on piazza
  • Reminder: Prep problems
    • The pattern

ReadPrep ProblemVideo / (optional) liveRepeat

3 of 52

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|

4 of 52

Comments

  • Lines starting in # are comments to the user
  • You can leave comments for yourself, of future readers of your code!

# 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.")

5 of 52

Comments

  • It is typical to put a comment at the top of all code files
  • This is called a header comment or file comment
  • You should do this for all of your programs, including the Movies PA

#

# Author: Benjamin Dicken

# Class: CSc 110

# Description: A program that . . .

#

6 of 52

Integers and Variables

  • We can also assign a name to a numeric value, instead of a string of characters
  • For the time being, we will be using integers
    • Integer: a number with no fractional or decimal representation

7 of 52

Integers and Variables

  • We can also assign a name to a numeric value, instead of a string of characters
  • For the time being, we will be using integers
    • Integer: a number with no fractional or decimal representation
  • For example:

age = 32

years_in_service = 17

wing_width = 25

8 of 52

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

9 of 52

Multiplying a string

  • You can use the asterisk ( * ) to repeat a string any number of times

10 of 52

Multiplying a string

  • You can use the asterisk ( * ) to repeat a string any number of times
  • For example

name = 'CSc' * 3

print(name)

11 of 52

Multiplying a string

  • You can use the asterisk ( * ) to repeat a string any number of times
  • For example

name = 'CSc' * 3

print(name)

CScCScCSc

12 of 52

Multiplying a string

  • You can use the asterisk ( * ) to repeat a string any number of times
  • For example

name = 'CSc' * 10

print(name)

CScCScCScCScCScCScCScCScCScCSc

13 of 52

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

14 of 52

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

15 of 52

Revisiting House printing

  • Write a program that allows us to print out a house of various widths
  • The user can tell the program how wide of a house to print

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|

16 of 52

Revisiting House printing

  • Write a program that allows us to print out a house of various widths
  • The user can tell the program how wide of a house to print

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!

17 of 52

Step 1

  • Write a program that just prints out one house size:

Here's a size 3 house:

___^___

/ \

| H |

|___H___|

Activity

18 of 52

Step 1

1 print("Here's a size 3 house:")

2 print(" ___^___")

3 print("/ \ ")

4 print("| H |")

5 print("|___H___|")

19 of 52

Step 2

  • Next, change the program to grab an input value
    • (Can still print just the size 3 house)

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

20 of 52

Step 2

1 size = input('What size house should be printed? ')

2 print(" ___^___")

3 print("/ \ ")

4 print("| H |")

5 print("|___H___|")

21 of 52

Step 3

  • Now, use this number to grow the width of the house

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

22 of 52

Step 3

  • Now, use this number to grow the width of the house

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?

23 of 52

Converting an input value to a string

Does not work:

width = input('enter width: ')

print('-' * width)

24 of 52

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)

25 of 52

What about the space?

  • How can you get the house to match the solution with commas?

26 of 52

What about the space?

  • How can you get the house to match the solution with commas?
  • Use string concatenation instead ( + )

27 of 52

What about the space?

  • How can you get the house to match the solution with commas?
  • Use string concatenation instead ( + )

print('_' * 5, '^', '_' * 5)

_____ ^ _____

print('_' * 5 + '^' + '_' * 5)

_____^_____

28 of 52

Step 3

  • Now, use this number to grow the width of the house

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

29 of 52

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 + '|')

30 of 52

Newlines

  • We’ve seen several escape sequences so far
  • An escape sequence is a sequence of characters that produces a particular character within a string
    • \' \" \\ What do these produce?

31 of 52

Newlines

  • We’ve seen several escape sequences so far
  • An escape sequence is a sequence of characters that produces a particular character within a string
    • \' \" \\ What do these produce?
    • And now: \n

32 of 52

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

33 of 52

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

34 of 52

Vertical Difference

  • In the house example, we had a program that could make houses of varying widths
  • What about a program that can produce output of varying heights?

35 of 52

Smiles

  • In this case, your program should take a number as input
  • Make the face taller or shorter, based on the input value

36 of 52

The Goal: Face printing

What size face should be printed? 0

///|\\\

/ 0 0 \

| I |

| \___/ |

\_____/

37 of 52

The Goal: Face printing

What size face should be printed? 1

///|\\\

/ 0 0 \

| |

| I |

| |

| \___/ |

\_____/

38 of 52

The Goal: Face printing

What size face should be printed? 3

///|\\\

/ 0 0 \

| |

| |

| |

| I |

| |

| |

| |

| \___/ |

\_____/

39 of 52

Implement the face program!

  • The space between the eyes and nose and nose and mouth changes with varying sized faces
  • You can use other characters for the eyes, nose, hair

Activity

40 of 52

Face Printing Step 1

  • Just get a face printing out
  • Don’t yet worry about adjusting the height

A face:

///|\\\

/ 0 0 \

| I |

| \___/ |

\_____/

Activity

41 of 52

Face Printing Step 2

  • Accept the input value and convert it to an int

What size face should be printed? 3

///|\\\

/ 0 0 \

| I |

| \___/ |

\_____/

Activity

42 of 52

Face Printing Step 3

  • Make the size adjustable!

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

43 of 52

height = int(input('input: '))

print(' ///|\\\\\\')

print('/ 0 0 \\')

print('| |\n' * height, end="")

print('| I |')

print('| |\n' * height, end="")

print('| \___/ |')

print(' \_____/')

44 of 52

Division in Python

  • What does it print out?

age = 35

half_age = age / 2

print('You' * half_age)

Activity

45 of 52

Two numeric types: float and integer

  • An integer is a type of value that can represent numbers without decimals or fractions
  • A float can represent numbers with decimals

age = 35 # integer

score = 81.23 # float

height_meters = 1.8288 # float (6 feet)

print(type(age))

print(type(score))

print(type(height_meters))

46 of 52

Division in Python

  • You can complete a division operation with the forward-slash ( / )
  • For example

age = 35

half_age = age / 2

47 of 52

Division in Python

  • What does it print out?

a = 52 / 7

e = 27 / 5

c = int(a)

print(e, '#' * c, c)

print('#' * c, e)

print(e, a, e)

Activity

48 of 52

5.4 ####### 7

####### 5.4

5.4 7.428571428571429 5.4

49 of 52

Flag

  • Write a program that takes one value as input
  • This value will be used to determine the width of a flag

Enter flag size: 10

#####-----

#####-----

----------

----------

Enter flag size: 20

##########----------

##########----------

--------------------

--------------------

Enter flag size: 30

###############---------------

###############---------------

------------------------------

------------------------------

Activity

50 of 52

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)

51 of 52

Flag (version B)

  • Write a program that takes TWO values as input
  • This value will be used to determine the width and height of a flag

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

52 of 52

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)