# Noemi Cabrera # 27 September 2021 # In this lesson, I learned to compare strings. It's important to remember "A" is less than "B" and lowercase "a" # is greater than uppercase "A". ALso, you can do this in if/else statements to help you write programs # effectively. # The difficulties I had were with writing the program in the last task. At first, I created a variable that got # input from the user and then I defined the function, which had an if/else statement. Either my program # displayed an error or it displayed what I didn't want. # After many tries, I figured out I had to include the return keyword in the if/else statements, and that I had # use the parameters I defined in the function's code without assigning them a value so that they could be # assigned a value when calling the function.
# [This comparison displays False because lowercase is always greater than uppercase] # review and run code "hello" < "Hello"
# [This comparison displays False because alphabetically A is less than Z, not greater.] # review and run code "Aardvark" > "Zebra"
# This comparison displays True because lowercase is not equal to upper case. # review and run code 'student' != 'Student'
# This code compares 'student'and 'Student' by using >= and !=. Both comparisions display true because student # is greater than Student, and it's not equal to Student. # review and run code print("'student' >= 'Student' is", 'student' >= 'Student') print("'student' != 'Student' is", 'student' != 'Student')
'student' >= 'Student' is True 'student' != 'Student' is True
# [In this code, the (==) sign is used to check if the 2 statements are equal. Since they are, True # will display] review and run code "Hello " + "World!" == "Hello World!"
# This code displays the results of comparing the variable msg and the string "Hello". # The == sign checks if they are equal and since they are, True displays. msg = "Hello" print(msg == "Hello")
# This code displays the results of testing if msg (input from user) equals the variable greeting. # Since the user is asked to say "Hello", both variables should contain "Hello, and they would be equal to each # other. Therefore, True displays" greeting = "Hello" msg = input('Say "Hello "') print('msg string equals greeting string: ', msg == greeting)
msg string equals greeting string: True
# [ In this code, msg is compared to "save the notebook" in an if/else statement. The (==) sign checks if they # are equal to each other. The msg variable is converted to lower case, which makes the two string equal to # each other. ] review and run code msg = "Save the notebook" if msg.lower() == "save the notebook": print("message as expected") else: print("message not as expected")
message as expected
# [ In this code, msg is compared to prediction in an if/else statement. The (==) sign checks if they # are equal to each other. The msg and prediction variables are converted to lower case, which makes the two equal # to each other. ] review and run code msg = "Save the notebook" prediction = "save the notebook" if msg.lower() == prediction.lower(): print("message as expected") else: print("message not as expected")
message as expected
# This code asks the user for the result of the sum 8+13. If the user, enters the correct answer, the if statement # will display. If the user enters the incorrect answer, the else statement will display. answer = input('What is 8 + 13?: ') if answer == "21": print( "You're correct. The answer is 21" ) else: print("Your answer is incorrect")
Your answer is incorrect
# This program get user input for a True/False question and displays whether the user was correct or incorrect. # First, tf_quiz is defined and it returns correct or incorrect depending on what the user entered. # Then, this function is called with the requested arguments, which are a question and the correct answer. # An statement saying if the user was correct is displayed. def tf_quiz(question, correct_ans): answer = input(question) if answer.upper() == correct_ans: return("correct") else: return("incorrect") quiz_eval = tf_quiz("(T/F) Plants grow without water", "F") print("your answer is", quiz_eval)
your answer is correct