Concepts
None
- is an object
- function:
return None
orreturn
or no return - can be used to set a variable
- is not the same as zero or blank
- can be as default for function argument
- this is beyond what we have covered in class.
- if parameter is mutable, does not retain the paramter value
- this is beyond what we have covered in class.
- homeworks with docstrings
While Loops¶
- loop body
- expression
- evaluated before entering the loop body
- iteration
- loop should terminate eventually
- boolean expression evaluates to false
- sentinel value
count = int(input("enter an integer number: "))
while count < 10 and count % 7 != 0:
print(count)
count += 1
count = int(input("enter an integer number: "))
while count < 10 and count % 7 != 0:
print(count)
count += 1
While loop and validating user input¶
amount = float(input("Enter Withdrawal Amount: "))
while (amount < 1.0 or amount > 300.0):
print("Amount must be $1.00 - $300.00.")
amount = float(input("Enter Withdrawal Amount: "))
print(f"Here is what you entered {amount:.2f}")
How many times is the loop body executed?¶
How many times is the boolean expression executed?¶
What is the output?
grades = [93, 82, 67, 99]
while len(grades) > 1:
min_grade = min(grades)
print(min_grade)
grades.remove(min_grade)
print(grades[0])
Tracing output: predict results on paper¶
# Problem 1
count = 1
while count <= 3:
count += 1
print(count)
# Problem 2
count = 7
while count > 3:
print(count)
count -= 2
# Problem 3
count = 10
while count > 0:
count += 1
print(count)
Random numbers¶
- want to generate pseudo random numbers
- seed based on current time
random
modulerandom.random()
: returns a float from 0.0(inclusive) to 1.0 (exclusive)random.randrange(x)
: returns an integers from 0 to x-1random.randrange(min, max)
: returns an integer from min to max-1random.randint(min, max)
: returns an integer from min to maxrandom.seed(number)
: can reproduce the series of random numbers