2.4 Repetition Assignment.pdf
# Repetition1
# Leen Saflo
# Purpose is to allow user to input password until correct
while True:
password = input("Enter your password: ")
if password == "Ilovecats123" :
print("Correct password")
break
else:
print("Incorrect password. Try again.")
# repetition2.py
# Leen Saflo
# To determine how long it'll take for population to double at current rate
population = 8017000000 # 8.017 billion
target_population = population * 2 # population doubled
growth_rate = 1.009 # 0.9% per year
years = 0
while population < target_population:
population *= growth_rate
years += 1
print ("It'll take", years ,"years for the population to double at this rate")
# repetition3.py
# Leen Saflo
# Display a menu and take user's order + asking for fries if they did not order it
print("""
----------- Taco Mambo ---------------
1. Inferno Crunch ...............$6.00
2. Baja Wave Rider...............$7.99
3. Veggie Vortex ................$5.00
4. Smoky Sunset..................$7.50
5. Fries ........... $4.00
6. Exit
---------------------------------------
""")
total = 0
fries_ordered = False
print("Welcome to Taco Mambo!")
while True:
order = int(input("From 1-5, what would you like to order <6 to exit>: "))
if order == 1:
total += 6
elif order == 2:
total += 7.99
elif order == 3:
total += 5
elif order == 4:
total += 7.50
elif order == 5:
total += 4
fries_ordered = True # Mark that fries have been ordered
elif order == 6:
break
else:
print("We don't have that, please select a number between 1 and 6.")
name = input("Enter a name for your order: ")
# Only ask for fries if they have not already been ordered
if fries_ordered == False:
wantFries = input(f"Would you like some fries with that (yes or no), {name}? ")
if wantFries.lower() == "yes":
total += 4
print(f"Your total is ${total}.2f thank you for coming to Taco Mambo, {name}!")
# repetition5.py
# Leen Saflo
# Find layers in a pyramid with 10,000 cannonballs
layers = 0 # number of layers
inLayer = 0 # number of cannonballs in layer
total = 0 # total number of cannonballs
while total < 10000:
layers += 1
inLayer = layers ** 2
total = total + inLayer
if total>10000: # stopping the loop before 10000
layers -= 1
total = total - inLayer
remainder = 10000 - total
print(F" there'll be {layers} layers and {remainder} cannonballs left over")
# repetition4.py
# Leen Saflo
# Find the two-digit number with persistence greater than 3
for num in range(10, 100):
persistencenum = 0
n = num # n will be the number i am working with
# calculating persistence by multiplying digits until it becomes a single digit
while num >= 10:
tens = num // 10 # first digit
ones = num % 10 # second digit
num = tens * ones # multiplying the digits
persistencenum += 1
# print the number with persistance > 3
if persistencenum > 3:
print(f"{n} is the only number with persistence above 3, it has a persistence of {persistencenum}")
# repetition4.py
# Leen Saflo
# Find the two-digit number with persistence greater than 3
for num in range(10, 100):
persistencenum = 0
n = num # n will be the number i am working with
# calculating persistence by multiplying digits until it becomes a single digit
while num >= 10:
tens = num // 10 # first digit
ones = num % 10 # second digit
num = tens * ones # multiplying the digits
persistencenum += 1
# print the number with persistance > 3
if persistencenum > 3:
print(f"{n} is the only number with persistence above 3, it has a persistence of {persistencenum}")
Repetition assignment.py