Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2022 Jan 8
- # WEBINAR: For Loops
- # We use loops to repeat actions
- # a WHILE loop... btw.. 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(item)
- myTuple = ("Gilligan", "Castaway002", "red", "crew")
- for item in myTuple:
- print(item)
- myString = "Just sit right back and you'll hear a tale."
- for char in myString:
- print(char, end=" ") # don't leave the cursor hanging here though!
- print() # next print() starts on a clean line
- # dictionaries
- # someDictionary = {"key": "value"}
- scoobiesDCT = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- # myDictionary[someKey]
- # myDictionary[someKey] = value
- for key in scoobiesDCT:
- print("{} wears {}.".format(key, scoobiesDCT[key])) # str.format()
- # print(f"{key} wears {scoobiesDCT[key]}.") # f string
- # print(str(key) + " wears " + scoobiesDCT[key] + ".") # concatenation with +
- # print("%s wears %s." % (key, scoobiesDCT[key])) # "data conversion specifiers" or "string modulo"
- for n in range(0, 5): # range() function creates a list-like container of those numbers
- print("hey")
- for i in range(0, len(myList)):
- print("{}: {}".format(i, myList[i]))
- # Lab 6.15 Password Modifier
- # There are different ways we could approach this one....
- word = input()
- password = "" # sitting there waiting for us to add the right chars...
- # You could just loop over the first string and use a long if/elif/else to check the chars
- for ltr in word:
- if ltr == 'i':
- ltr = '1'
- password += ltr
- if ltr == 'm':
- ltr = 'M'
- password += ltr
- if ltr == 'B':
- ltr = '8'
- password += ltr
- if ltr == 's':
- ltr = '$'
- password += ltr
- else:
- ltr == ltr
- password += ltr
- password += '!'
- print(password)
- # OR... this would be a good place to use a dictionary!
- newLetters = {
- "i": "1",
- "a" : "@",
- "m" : "M",
- "B" : "8",
- "s" : "$"
- }
- for ltr in word:
- if ltr in newLetters: # checking ltr as a key in the dictionary
- password += newLetters[ltr]
- else:
- password += ltr
- # and again AFTER the loop...
- password += '!'
- print(password)
- # A great student suggestion here: you could use the str replace() method and not even need to loop
- password = word.replace("i", "1")
- password = word.replace("a", "@") # and so on...
- # Note that since str.replace() RETURNS a string itself, you can daisy-chain those methods:
- password = word.replace("i", "1").replace("a", "@").replace("m", "M") #... and so on again!
- # just don't forget to add the "!" at the end again
- # or combining the dictionary and .replace() approaches
- for ltr in word:
- if ltr in newLetters: # checking ltr as a key in the dictionary
- # if you're using the original input variable, don't forget to re-assign!
- word = word.replace(ltr, newLetters[ltr])
- # again add the "!" and print
- # All of these are good ways to approach this problem!
- # Calling help() and dir() in the Labs, Pre, and OA:
- help(str) # whole help doc on strings
- print(dir(str)) # dir() returns a list, fyi
- help(str.find) # so see help on just this method
- # Want to weed out the "__" special attributes?
- for item in dir(str):
- if not item.startswith("_"):
- print(item)
- # help() and dir() are your friends!
Add Comment
Please, Sign In to add comment