Python Programming: An Introduction To Computer Science: Loop Structures and Booleans
Python Programming: An Introduction To Computer Science: Loop Structures and Booleans
An Introduction
To Computer Science
Chapter 8
Loop Structures and Booleans
def main():
n = eval(input("How many numbers do you have? "))
sum = 0.0
for i in range(n):
x = eval(input("Enter a number >> "))
sum = sum + x
print("\nThe average of the numbers is", sum / n)
Note that sum is initialized to 0.0 so that sum/n returns a float!
def main():
moredata = "yes"
sum = 0.0
count = 0
while moredata[0] == 'y':
x = eval(input("Enter a number >> "))
sum = sum + x
count = count + 1
moredata = input("Do you have more numbers (yes or no)? ")
print("\nThe average of the numbers is", sum / count)
def main():
sum = 0.0
count = 0
x = eval(input("Enter a number (negative to quit) >> "))
while x >= 0:
sum = sum + x
count = count + 1
x = eval(input("Enter a number (negative to quit) >> "))
print("\nThe average of the numbers is", sum / count)
def main():
sum = 0.0
count = 0
xStr = input("Enter a number (<Enter> to quit) >> ")
while xStr != "":
x = eval(xStr)
sum = sum + x
count = count + 1
xStr = input("Enter a number (<Enter> to quit) >> ")
print("\nThe average of the numbers is", sum / count)
def main():
fileName = input("What file are the numbers in? ")
infile = open(fileName,'r')
sum = 0.0
count = 0
for line in infile.readlines():
sum = sum + eval(line)
count = count + 1
print("\nThe average of the numbers is", sum / count)
def main():
fileName = input("What file are the numbers in? ")
infile = open(fileName,'r')
sum = 0.0
count = 0
line = infile.readline()
while line != "":
sum = sum + eval(line)
count = count + 1
line = infile.readline()
print("\nThe average of the numbers is", sum / count)
import string
def main():
fileName = input("What file are the numbers in? ")
infile = open(fileName,'r')
sum = 0.0
count = 0
line = infile.readline()
while line != "":
for xStr in line.split(","):
sum = sum + eval(xStr)
count = count + 1
line = infile.readline()
print("\nThe average of the numbers is", sum / count)
P Q P and Q
T T T
T F F
F T F
F F F
Python Programming, 2/e 51
Boolean Expressions
In the truth table, P and Q represent
smaller Boolean expressions.
Since each expression has two possible
values, there are four possible
combinations of values.
The last column gives the value of P
and Q.
P Q P or Q
T T T
T F T
F T T
F F F
P not P
T F
F T
DeMorgan’s laws:
not(a or b) == (not a) and (not b)
not(a and b) == (not a) or (not b)