Lecture Notes on 10 Feb 2017
Active Reading: Type code as you read.
Read Chapter 5: Sections 5.7 - 5.8
Write the code from the following listings and run them:
* Listing 5.11: TestBreak.py
* Listing 5.12: TestContinue.py
* Listing 5.13: PrimeNumber.py
def main():
# prompt the user to enter a positive integer
num = int (input ("Enter a positive integer: "))
# prompt user repeatedly if negative number is entered
while (num <= 0):
num = int (input ("Enter a positive integer: "))
# treat 1 as a special case
if (num == 1):
print (num, "is not prime")
return
# define the limit for iteration
limit = int (num ** 0.5) + 1
# try all divisors within the limit
divisor = 2
is_prime = True
while (divisor < limit):
if (num % divisor == 0):
is_prime = False
break
divisor += 1
# print the result
if (is_prime):
print (num, "is prime")
else:
print (num, "is not prime")
main()
Challenge Problem:
There is a trucking companing that has a fleet of trucks that operate
between Austin and Dallas. The company has a truck leave Austin on the
hour every hour for Dallas. There is also a truck that leaves Dallas
on the hour every hour for Austin. The journey between Austin and Dallas
takes 4 hours. How many trucks of the parent company does an operator
see on his drive on I-35 from Austin to Dallas?