Class 06 – Friday, February 12
Fearlessly we march on — variables and strings
A sign for knowing — what the future asks of us — demands we act now
Help crush breast cancer — For your friends and families— Not be asunder
Look both ways
Agenda
- Help you better model how a computer works
- Consider assignment subtleties
- Continues introduction to the string type
str
- Advance your chrestomathics abilities
Examples
- Program who_what_and_where.py
- shows what Python keeps track of with variables
- Program the_old_switcheroo.py
- Considers the subtlety of swapping variable values
- Program escaping.py
- Explores the use of Python escape character
\
for representing special characters.
- Program i_need_water.py
- Explores limits on string building
- Program whats_the_point.py
- Shows use of function
round()
Inquiring minds want to know
Program who_what_and_where.py
- Introduces built-in functions
id()
andtype()
.
id( n )
identifies where in memory to findn
type( n )
supplies what type of value isn
-
Three possible program runs
Enter word: dabchicks
Enter integer: 1112
Enter decimal: 3.14
w: dabchicks
si: 1112
sd: 3.14
w is <class 'str'> and is at 140270905125744 and when printed displays dabchicks
si is <class 'str'> and is at 140270778326512 and when printed displays 1112
sd is <class 'str'> and is at 140270778326192 and when printed displays 3.14
i: 1112
d: 3.14
i is <class 'int'> and is at 140270779823408 and when printed displays 1112
d is <class 'float'> and is at 140270905399376 and when printed displays 3.14
Enter word: hydroxyzine
Enter integer: 2020
Enter decimal: 9.7
w: hydroxyzine
si: 2020
sd: 9.7
w is <class 'str'> and is at 140690645154672 and when printed displays hydroxyzine
si is <class 'str'> and is at 140690518355696 and when printed displays 2020
sd is <class 'str'> and is at 140690518355376 and when printed displays 9.7
i: 2020
d: 9.7
i is <class 'int'> and is at 140690519852336 and when printed displays 2020
d is <class 'float'> and is at 140690645432400 and when printed displays 9.7
Enter word: 1112
Enter integer: 1112
Enter decimal: 1112
w: 1112
si: 1112
sd: 1112
w is <class 'str'> and is at 139627077196656 and when printed displays 1112
si is <class 'str'> and is at 139626950397552 and when printed displays 1112
sd is <class 'str'> and is at 139626950397680 and when printed displays 1112
i: 1112
d: 1112.0
i is <class 'int'> and is at 139626951894320 and when printed displays 1112
d is <class 'float'> and is at 139627077474384 and when printed displays 1112.0
d is <class 'float'> and is at 140565240115512 and when printed displays 1112.0
Program the_old_switcheroo.py
- Purpose: swap the values of two variables
-
As miswritten
reply = input( 'Enter two words: ' )
w1, w2 = reply.split()
print( )
print( 'w1 =', w1 )
print( 'w2 =', w2 )
print()
# swap the values of w1 and w2
w1 = w2
w2 = w1
# print results
print( 'After swapping' )
print()
print( 'w1 =', w1 )
print( 'w2 =', w2 )
-
A program run — what is wrong?
Enter two words: wahoo wah
w1 = wahoo
w2 = wah
After swapping
w1 = wah
w2 = wah
Program escaping.py
- Explores the use of Python escape character
\
for representing special characters.
Program output
### print strings
a = 'petrichor'
b = 'a b'
c = 'a\tb'
d = 'aren\'t'
a: 'petrichor'
b: 'a b'
c: 'a b'
d: 'aren't'
len( a ): 9
len( b ): 4
len( c ): 3
len( d ): 6
Program i_need_water.py
- Explores limits on string building
water = 'H' + 2 + '0'
print( water )
water = 'H' + str( 2 ) + 'O'
print( water )
Program whats_the_point.py
- Explores function
round()
Enter number: 3.1415926
number: 3.1415926
round( number, 6 ): 3.141593
round( number, 5 ): 3.14159
round( number, 4 ): 3.1416
round( number, 3 ): 3.142
round( number, 2 ): 3.14
round( number, 1 ): 3.1
round( number, 0 ): 3.0
round( number ): 3
Program all_consuming.py — due Tuesday February 16
- Develop a program that estimates your annual consumption of a commodity based on how much you consume a typical weekday and a typical weekend day.
-
Assumptions
- The term week denotes 5 weekdays and 2 weekend days.
- Assume a year is exactly 52 weeks (yes, we know a real year is longer, but we will ignore that in this program).
- User supplied values are integers.
-
Requirements
- The program first prompts the user for the commodity consumed.
- The program then individually prompts the user for two integer inputs: first, the quantity consumed on a typical weekday, then the quantity consumed on a typical weekend day. These are to be requested using two separate prompts.
- The program uses the inputs to compute and report the estimate of the quantity consumed in a year.
- The quantity output should specify the commodity whose use is being reported.
-
Suggested algorithm
- Get input commodity of interest
- Get weekday consumption
- Get weekend day consumption
- Compute weekly consumption
- Compute yearly consumption
- Report yearly consumption
-
Three possible program runs
What is something you consume daily? paper napkins
How much consumed on a typical weekday: 6
How much consumed on a typical weekend day: 0
You consume 1560 paper napkins per year.
What is something you consume daily? pretzels
How much consumed on a typical weekday: 0
How much consumed on a typical weekend day: 1
You consume 104 pretzels per year.
What is something you consume daily? jelly beans
How much consumed on a typical weekday: 4
How much consumed on a typical weekend day: 12
You consume 2288 jelly beans per year.
To do list
- Review class artifacts
- Ensure familiarity by reading about Python variables, assignment, and input
- Read about Python string capabilities.
- Check out the epistles on Python and problem solving
Homework 5 commentary
- Seven people did not submit the assignment.
- Code snippets from submissions
real_age = input()
age=input("prompt:")
print( "Hello, how old are you?")
print('Enter your age: ')
print(min_dateability)
print( "You don't even look " + str(new_age))
print( "You don't look a day over " + str(supposed_age) )
print('You look like you are ' + str(supposed_age) + '.')
print(" You are " + str(age) + " years old? You don't look a day over " + str(fake_age) + "!")
print( 'You don\'t even look ' + fake_age_string )
print( 'Wow! You do not even look ' + faux_age_str + '!')
print("You do not even look " + fake_age_string)
print( "You don't even look " + result )
print("You look even better than someone who is " + int(faux_age) + " years old!")
print("You look even better than someone who is ", int(faux_age), " years old!")
print('You dont look a day over', f )
print('Oh! I thought you were ', f)
print("Impossible. You don't look a day over" ,r)
print('Have we gone back in time?? You look', x )
print( 'you look great for your age, you look no older than', y)
print( 'You do not even look', b )
print('You do not even look',n)
print('Wow, you dont even look',y)
print( 'You don\'t even look', final)
print('you don\'t even look' , s_age ,'!')
print( 'You dont even look', value )
print('You dont even look', beautyage)
print('Really? You only look', fauxage)
print("Wow! You don't look a day past", youngerage)
print( "you don't look like you're", supposedage)
print('Wow! You hardly look',fauxage)
print('You do not look a day over', youngerage)
print("You don't even look", Ageminusfive)
print("You don't even look ", younger_Age)
print('You do not even look', Faux_age)
print('You do not look a day over', Pho_age)
print( "No way! At most,", faux_age, "in dog years, more like!" )
All you need is love — All you need is love — All you need is love, love — Love is all you need. The Beatles
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |