Lesson 2.4– Loops.pdf
exercises 2.4.pdf
#repetitionEx1.py
fail = 0
classes = int(input("enter # of classes: "))
for i in range (classes):
mark = int(input("enter mark: "))
if mark <= 50:
fail += 1
print (f"you are failing {fail} classes")
#repetitionEx2.py
total_squares = 64
grains_per_pound = 7000
total_grains = 0
grains_on_square = 1
for square in range(total_squares):
total_grains += grains_on_square
grains_on_square *= 2
total_pounds = total_grains / grains_per_pound
print(f"The total number of grains is: {total_grains}")
print(f"The total weight in pounds is: {total_pounds:.2f}")
#repetitionEx3.py
joe = int(input("ballots for joe: )"))
bob = int(input("ballots for bob: )"))
bill = int(input("ballots for bill: )"))
if joe>bob and joe> bill:
print("joe won")
elif bob>joe and bob> bill:
print("bob won")
if bill>bob and bill> joe:
print("bill won")
else:
print("there is a tie")
total = joe + bob + bill
print(f" bob earned {bob/total:.2f}% of the votes")
print(f" joe earned {joe/total:.2f}% of the votes")
print(f" bill earned {bill/total:.2f}% of the votes")
#repetitionEx4.py
distance = 384400000 # in meters
folds = 0
length = 0.0001 # in meters
total = 0
while total < distance:
folds += 1
length *= 2
total += length
print (f" it'll take {folds} to reach the moon")