# Noemi Cabrera # 1 October 2021 # In this lesson, I practiced using nested conditionals, as well as formatting output with the escape # sequences built in Python. All escape sequences use the backslash(\) # The difficulties I had were with creating the program in Task 2. My program didn't do what I wanted. # At first, when writing the if/else condition for the function, I used the word pass in some of the nested # conditions I created, which skipped some of the steps needed. Then, I realized I could put an if statement # below the first if statement I created, which solved the problem I had.
# [ In this code, the \n escape sequence is used. This creates a new line after Hello World! ] # review and run example using \n (new line) print('Hello World!\nI am formatting print ')
Hello World! I am formatting print
# [ In this code, the \t escape sequence is used. This creates a tab or space in between the string and # the variables used in this code.] review and run code using \t (tab) student_age = 17 student_name = "Hiroto Yamaguchi" print("STUDENT NAME\t\tAGE") print(student_name,'\t' + str(student_age))
STUDENT NAME AGE Hiroto Yamaguchi 17
# [ In this code, the \" , \', and \\ escape sequences are used. The backslash is used before a single or # double quote to display it. If a backslash is used twice, one backslash will display. ] review and run code # using \" and \' (escaped quotes) print("\"quotes in quotes\"") print("I\'ve said \"save your notebook,\" so let\'s do it!") # using \\ (escaped backslash) print("for a newline use \\n")
"quotes in quotes" I've said "save your notebook," so let's do it! for a newline use \n
# This code uses the escape sequences \" and \\ to display the message. To display quotes, \" is used. # To display a backslash, \\ is used. print( "\"\\\\WARNING!///\"")
# This code uses the escape sequences \" and \' to display quotes when printing the string. print( "\"What\'s that?\" isn\'t a specific question." )
"What's that?" isn't a specific question.
# This code usea \n to put the rest of the string next to it in a new line. The \t sequence adds a tab(space) # in between the words. print("One\tTwo\tThree\nFour\tFive\tSix")
One Two Three Four Five Six
#[ This program asks the user to enter a word that starts with "pre". # The pre_word() function returns true if the word entered starts with "pre" and only has alphabet letters, # and false if it doesn't. # When the pre_word() function is called, the if/else statement is used to print if the word entered was # valid or not. ] create and test pre_word() word = input("Enter a word that starts with \"pre\": ").lower() def pre_word(pre): if word.startswith("pre"): if word.isalpha(): return True else: return False else: return False if pre_word("press") == False: print("What you entered is not a valid \"pre\" word") else: print("You have succesfully entered a word that starts with \"pre\"")
You have succesfully entered a word that starts with "pre"
# [ This code displayed a syntax error. The error was that the escape sequence \n was not used within the string # and instead was added with the + sign. To fix this, I combined the 2 strings in one and then I added \n in # between the words Hello and World..] review, run, fix - comment your fixes print("Hello\nWorld!")