# Noemi Cabrera # 8 October 2021 # In this lesson, I learned and practiced creating while forever loops using while and break. Also, incrementing # variables in a while loop is very useful when you want to add input multiple times and display the result in a # program. You can control while loops using Boolean operators and string tests. # The difficulties I had were with creating the program in Task 1. Mostly, I had trouble with printing all the # animal names entered by the user, if any names were entered. I searched for ways to do this and what worked # for me was to increment the all_animals variable with a string plus the variable animal_name, which gets # input. However, I wonder if there is another option to do this.
# [In this code, the while loop will run unless the green_count number is less than 0. The variable greet_count # is decremented by 1 every time. Once the while loop stops, the print statement below runs.] # review and run GREET COUNT greet_count = 5 # loop while count is greater than 0 while greet_count > 0: print(greet_count, "!") greet_count -= 1 print("\nIGNITION!")
5 ! 4 ! 3 ! 2 ! 1 ! IGNITION!
# [ This program gets user input only for 4 animal names. If the user enters 4 names, the loop breaks and a # statement with the animals entered is displayed. If the input is only exit, then a stament saying no animals # were entered will display. Lastly, if the user enters less than four names, a statement saying no more animals # are added and the names entered by the user are displayed.] Create the Animal Names program, run tests num_animals = 4 all_animals = '' while num_animals > 0: animal_name = input('Please enter the name of an animal or "exit" to leave: ').lower() if animal_name.startswith("exit"): print("No more animals entered\n") break else: print(animal_name,"added") all_animals += ' ' + animal_name.title() num_animals -= 1 if all_animals == '': print("no animals entered") else: print("Animals entered:",all_animals)
cat added dog added bunny added horse added Animals entered: Cat Dog Bunny Horse
# [In this code, a variable is first initialized. Then, the while loop is controlled by using the boolean test # that checks if the name entered by the user has only alphabetical characters. If this is false, then the code # will run until a valid name is entered. If this is true, the loop breaks and a statement is displayed. ] # review and run example that loops until a valid # first name format is entered student_fname = "" while student_fname.isalpha() == False: student_fname = input("enter student\'s first (Letters only, No spaces): ") print("\n" + student_fname.title(),"has been entered as first name")
Noemi has been entered as first name
# [ In this program, the boolen string test that checks if the input is digits is used. If the input is all # digits, the while loop will run and the user will be forever asked to enter a number until the input is not all # digits and fails the boolean test. ] Create the program, run tests int_num = input('Enter a number: ') long_num = '' while int_num.isdigit(): long_num = long_num + int_num int_num = input('Enter a number: ') print(long_num)
# [ This code had a logic error since there is no error message but the loop created never runs. # To fix this, I read through the code and realized that the loop doesn't run because the variable count # has to be greater than 6 for it to execute. I placed a less than(<) boolean comparison operator before 6 # instead of (>)] # review the code, run, fix the Logic error count = 1 # loop 5 times while count < 6: print(count, "x", count, "=", count*count) count +=1
1 x 1 = 1 2 x 2 = 4 3 x 3 = 9 4 x 4 = 16 5 x 5 = 25