Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WEBINAR: For Loops
- # We use loops to repeat actions
- # a WHILE loop is basically an IF statement
- # that repeats as long as its condition is true
- # FOR LOOPS are used for repeating actions for every element
- # in a container like a list, string, tuple, etc...
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- for item in myList:
- # print("The truth is out there, {}!".format(item)) # string.format()
- # BTW you'll see F STRING formatting in the new book and Pre
- # It's very similar to string.format(), just more condensed...
- print(f"The truth is still out there, {item}!")
- # Student questions
- #Lab 26.3.1
- # input
- user_input = "automobile car manufacturer maker children kids"
- user_sentence = "The automobile manufacturer recommends car seats for children if the automobile doesn't already have one."
- ''' Type your code here. '''
- # help(str.replace)
- # for item in dir(list):
- # print(item)
- word_pairs = {}
- # grab the words to replace
- tokens = user_input.split()
- # fill word pairs dict
- for i in range(0, len(tokens), 2):
- # word_pairs[key] = value
- word_pairs[tokens[i]] = tokens[i+1]
- # print(f"dictionary: {word_pairs}") # it looks good!
- # You could... loop over dict to do our string replacement
- # for k,v in word_pairs.items():
- # user_sentence = user_sentence.replace(k, v)
- # an alternate way...
- # Loop over the sentence as a list of string instead, checking dict keys as you go:
- for word in user_sentence.split(): #
- if word in word_pairs:
- user_sentence = user_sentence.replace(word, word_pairs[word])
- # check string out
- print(user_sentence)
- # find day of the week from a specified date
- import datetime
- dd = datetime.datetime(1971, 3, 3) # what day of the week?
- for item in dir(datetime.datetime):
- if not item.startswith("_"):
- print(item)
- help(datetime.datetime.weekday)
- print(dd.weekday()) # day is 2... OK, which day is 2?
- import calendar
- print(calendar.day_name[dd.weekday()])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement