Leftovers
Make us!
* ** *** **** *****
* *** ***** ******* *********
* * * * * * * * * * * * * * * * * * * * * * * * *
See the programs arrow.py
and triangle.py
on the left for a menagerie of solutions.
Using input
The input
function takes a string as an argument. This string is
put the screen to prompt the hapless user to enter something
Whatever the user types is returned as a string.
age = input("Enter your age: ")
print(age)
print(type(age))
age = int(age) ##input returns a string!
print(f"The square of your age is {age*age}")
Here we run the program. The $ sign stands for your system's prompt.
$ python get_info.py Enter your age: 45 45 <class 'str'> The square of your age is 2025
Example: Have a user enter two numbers (such as 7 and 8) and have the program print 7*8 = 56 to the screen.
Here is the solution, wabbit.py
.
#4*5 = 20
#Amy
number_1 = input("Enter a number: ")
#Ana
number_1 = int(number_1)
#Evan
number_2 = int(input("Enter a second number: "))
#Tristen
print(f"The product of your two chosen numbers is, {number_1} * {number_2} = {number_1*number_2}")
The Grand Scheme: Python Expressions
Wormwood on Steroids
What is the order of operations for a general Python expression?
Operator Types
#1 arithmetic + - * // / % () function() wormwood #4 assignment = #2: relationals: < > <= >= == != #3: boolean and, or, notHere we evaluate some expressions
x = 7 x = x*2 + 4/5 x = 7*2 + 4/5 substitute x = 14 + .8 multiply x = 14.8 add Assignment makes x poiont at 14.8. x = 7 y = 2 z = x < 7 or y*y > 3 z = 7 < 7 or 4 > 3 substitute z = False or True do relational tests z = True do boolean operations z now points at True. z = 4 z = z*4 + 8*7 z = 4*4 + 8*7 substitute z = 16 + 56 multipwy, elmer z = 72 add z now points at 72
Compound Asssignment Operators These two are the same.
x = x op blah (op is an infix binary operator) x op=blah
Here is compound assignmment at work.
>>> x = 13
>>> x += 5
>>> x
18
>>> x *= 4
>>> x
72
>>> x //=10
>>> x
7
>>>