# Noemi Cabrera # 8 October 2021 # In this lab, I practiced using my knowledge about while forever loops by creating different programs # and writing code to ask for specific input. I incremented and decremented variables in the code. I controlled the # loops by using boolean string tests and operators. All of thse are helpful to write even more effective code # and programs. # The difficulties I had were mostly asociated with the last task where you have to create a function. In the code, # I asked the user a specific question, which didn't allow me to ask more questions when calling the function as it # was instructed. To fix this, I placed the parameters' name in the code, which then allowed me to insert any # question and the correct answer of it.
# [ In this code, the user is asked to enter a number to make a sum. If the user enters a non-digit term such as # word or space, the loop will break and the sum will be displayed. ] use a "forever" while loop to get user input of integers to add to sum, # until a non-digit is entered, then break the loop and print sum sum = 0 while True: int_input = input("Enter a whole number to make a sum: ") if int_input.isdigit(): int_input = int(int_input) sum += int_input else: print(sum) break
# [ In this code, the user is asked to guess a color in a rainbow. If the the user guessed the color, # the while loop ends and a message displays. But if the guess is not correct, the user will be asked to enter # another color until the four guesses run out. ] use a while True loop (forever loop) to give 4 chances for # input of a correct color in a rainbow # rainbow = "red orange yellow green blue indigo violet" correct_color = "violet" num_guess = 4 while num_guess > 0: color_guess = input("Guess a color found in a rainbow\n(red, orange, yellow, green, blue, indigo, or violet):").lower() if color_guess == correct_color: print("You guessed right.\n", correct_color,"is the correct color") break else: num_guess -= 1
# [ In this code, the user is asked to enter a proper book title. If the format is correct, the loop breaks # and a message is displayed. But if the title entered is not in the correct format, the user will be asked # to enter the title again. ] # Get input for a book title, keep looping while input is Not in title format (title is every word capitalized) title = "" while True: book = input("Enter a correct book title (the first letter of each word should be capitalized): ") if book.istitle(): print(book, "is in the correct format !") break else: title += book
Lulu And Her Friend is in the correct format !
# [ In this code, the user is asked to answer a math quiz question. If the user guesses the answer, then while # loop ends and a message is displayed. But if the user enters the incorrect answer, the loop will keep executing # until the correct answer is entered. ] create a math quiz question and ask for the solution until the input # is correct solution = "84" while True: quiz = input("Enter the result of 42 times 2: ") if quiz.isdigit() != True: print("You have to enter a number solution. Try again") elif quiz == solution: print("\nCongrats!! 84 is correct.") break else: print("Keep trying")
Congrats!! 84 is correct.
# [ The error in this code was that the string of the user input is compared to a number, which cannot be done. # To fix this, I casted the input to string in the variable before it's compared in the while loop and removed # the int() from the tickets variable when used in the while loop. ] # review the code, run, fix the error tickets = int(input("enter tickets remaining (0 to quit): ")) while tickets > 0: # if tickets are multiple of 3 then "winner" if tickets/3 == tickets/3: print("you win!") else: print("sorry, not a winner.") tickets = int(input("enter tickets remaining (0 to quit): ")) print("Game ended")
you win! Game ended
# This function asks the user to enter the answer to a question that has correct answer. If the user puts in # the correct answer, then True will be displayed. If not, the user will be asked to try again to get the right # answer. # Create quiz_item() and 2 or more quiz questions that call quiz_item() def quiz_item(question, solution): answer = input(question) while True: if answer == solution: return True break else: print("Try again") answer = input(question) quiz_item("Are potato chips good for you?","F") quiz_item("Do you need to eat fruits and veggies?","T") quiz_item("Do you need to drink at least 6 glasses of water per day?","T")