# Noemi Cabrera
# 6 October 2021
# In this lesson, I learned to create forever loops using while(), which can be ended by using
# the break keyword. The variables you want use in the code are initialized at the beginning
# so that you can use them when you want to increment or decrement their value. The Boolean
# operator True is used along with the while() to indicate a forever loop.
# I didn't have any major difficulties in this lesson. I made mistakes such as not including the
# colon at the end of a condition and indenting incorrectly when defining a function.
# [The while True loop will loop forever the statement that is indented. But since the break
# keyword is at the end, the statment does not loop and is displayed 1 time. ]
# Review and run code
# this example never loops because the break has no conditions
while True:
print('write forever, unless there is a "break"')
break
# [ In this code, the user is asked to guess a number between 1 and 5. If the user enters the
# secret number, then it will display an statement saying it's correct one time because break
# stops the while condition from repeating forever. However, if the user doesn't guess the secret
# number, the else statement will print and the while condition will repeat itself until the user
# guesses the number. ] review the NUMBER GUESS code then run - Q. what causes the break
# statement to run?
number_guess = "0"
secret_number = "5"
while True:
number_guess = input("guess the number 1 to 5: ")
if number_guess == secret_number:
print("Yes", number_guess,"is correct!\n")
break
else:
print(number_guess,"is incorrect\n")
# [ In this code, the user is asked enter the weather. If the user enters a existing weather,
# then a statement indicating what you should wear for that weather appears 1 time because break
# stops the while condition from repeating forever. However, if the user doesn't enter a valid
# weather, the else statement will print and the while condition will repeat itself until the user
# enters a valid weather. ] review WHAT TO WEAR code then run testing different inputs
while True:
weather = input("Enter weather (sunny, rainy, snowy, or quit): ")
print()
if weather.lower() == "sunny":
print("Wear a t-shirt and sunscreen")
break
elif weather.lower() == "rainy":
print("Bring an umbrella and boots")
break
elif weather.lower() == "snowy":
print("Wear a warm coat and hat")
break
elif weather.lower().startswith("q"):
print('"quit" detected, exiting')
break
else:
print("Sorry, not sure what to suggest for", weather +"\n")
# [ This program asks for user input on a common name. If the name is not alphabetical or is blank,
# the code would repeat until the user enters a vaid name that can be used. ]
# create Get Name program
familiar_name = ""
while True:
familiar_name = input( "Enter a common name of friends or family: " ).title()
if familiar_name.isalpha():
print("Welcome to our group", familiar_name,"!")
break
else:
print(familiar_name, "is not a valid name")
# [ In this code, the votes variable is initialized and assigned the value 3. The value is
# added 1 by writing votes + 1. Then the value is added 2 by wrting the short method:
# votes += 2. ] review and run example
votes = 3
print(votes)
votes = votes + 1
print(votes)
votes += 2
print(votes)
# Since the variable votes was initialized in the code above, here it's used again. The last value
# of the variable was 6. Then 6 is substracted 1 using the short method, which results in 5.
print(votes)
votes -= 1
print(votes)
# [ In this code using the while condition, the variable seat_count is increased by one repeatedly
# until the value becomes greater or equal than 4. The if statemnet contains the keyword break,
# which ends the forever loop.] review the SEAT COUNT code then run
seat_count = 0
while True:
print("seat count:",seat_count)
seat_count = seat_count + 1
if seat_count > 4:
break
# [ In this while condition, the user is asked to enter a seat type. If the input starts with e,
# the code displays a space and a statement indicating there are 0 seats entered. However, if the
# answer is either hard or soft, the value of 0 is added 1 repeatedly until the entries are
# equal to or greater than 4. If the entry is invalid, it's counted as hard and is also added
# 1 repeadtly until it reaches 4.]
# review the SEAT TYPE COUNT code then run entering: hard, soft, medium and exit
# initialize variables
seat_count = 0
soft_seats = 0
hard_seats = 0
num_seats = 4
# loops tallying seats using soft pads vs hard, until seats full or user "exits"
while True:
seat_type = input('enter seat type of "hard","soft" or "exit" (to finish): ')
if seat_type.lower().startswith("e"):
print()
break
elif seat_type.lower() == "hard":
hard_seats += 1
elif seat_type.lower() == "soft":
soft_seats += 1
else:
print("invalid entry: counted as hard")
hard_seats += 1
seat_count += 1
if seat_count >= num_seats:
print("\nseats are full")
break
print(seat_count,"Seats Total: ",hard_seats,"hard and",soft_seats,"soft" )
# [ The defined shirt_count function gets user input on the amount of shirts of the size S,M, and
# L wanted. The amount of each shirt is added 1 each time, but if the user enters exit, the
# loop will break and the number of purchased shirts is displayed. The else statement will display
# if the user input is not s,m,l, or exit. ] Create the Shirt Count program, run tests
def shirt_count():
s = 0
m = 0
l = 0
while True:
data = input("Enter shirt size (S,M,L, or Exit): ").lower()
if data == "s":
print("Adding a shirt size S")
s += 1
elif data == "m":
print("Adding a shirt size M")
m += 1
elif data == "l":
print("Adding a shirt size L")
l += 1
elif data == "exit":
print("No more shirts added.")
print("You purchased\n",s, "Small\n",m,"Medium\n",l,"Large")
break
else:
print("The shirt size you entered is not valid\nTry again")
shirt_count()
# [ This updated shirt_count function gets user input on the amount of shirts of the size S,M, and
# L wanted. The amount of each shirt is added 1 each time. If the user enters exit, the
# loop will break and the number of purchased shirts is displayed along with the subtotal cost of
# each shirt size and the total cost of the purchase. The else statement will display if the user
# input is not s,m,l, or exit. ] Create the Shirt Register Challenge program, run tests
def shirt_register():
s = 0
m = 0
l = 0
while True:
data = input("Enter shirt size (S,M,L, or Exit): ").lower()
if data == "s":
print("Adding a shirt size S")
s += 1
elif data == "m":
print("Adding a shirt size M")
m += 1
elif data == "l":
print("Adding a shirt size L")
l += 1
elif data == "exit":
print("No more shirts added.")
print("You purchased\n",s, "Small\n",m,"Medium\n",l,"Large")
print()
print("Subtotal:\nSmall = $"+str(s*6)+"\nMedium = $"+ str(m*7)+"\nLarge = $"
+ str(l*8))
print()
total = (s*6)+(m*7)+(l*8)
print("Total: $"+ str(total))
break
else:
print("The shirt size you entered is not valid\nTry again")
shirt_register()