Iterations and 2D Array
Unit 3 Sections 8 and 10 lesson
sports = ["football", "soccer", "baseball", "basketball"]
# change the value "soccer" to "hockey"
sports.remove("soccer")
sports.insert(1, "hockey")
print (sports)
sports = ["football", "soccer", "baseball", "basketball"]
sports.insert(2, "golf")
# add "golf" as the 3rd element in the list
print (sports)
words = ["alfa", "beta", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "julitt", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "wiskey", "xray", "yankee", "zulu"]
# Get ASCII value of char 'a'
ascii_a = ord('a');
# Get input from terminal
print("Please type in a word (alphabets ONLY) ...")
in_string = input()
# Convert the input string into all lower case
in_string = in_string.lower()
print(in_string + " ->" + "\n")
# Find ASCII value of each "char" in the "string"
# The difference of ASCII value between these characters and "a"
# will become the index to the list of words
for k in range(len(in_string)) :
in_ascii = ord(in_string[k]) - ascii_a
print(words[in_ascii])
keypad = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[" ", 0, " "]]
def print_matrix3(matrix):
for i in matrix:
print(*i)
print_matrix3(keypad)
def lookup_keyboard(msg) :
# Create a virtual keyboard by 2-D array
# - It is actually a list, with 2-dimenstion
# - In total it has 4 rows, each row has different number of list elements
keyboard = [["`", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "-", "="],
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]"],
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'"],
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"] ]
for k in range(len(msg)) : # Loop each letter in mybirthday
bb = msg[k] # Get the k-th letter in mybirthday
if bb.isnumeric() : # Convert it to a number if it is numeric
bb = int(bb)
for i in range(4) : # Loop through each row of keyboard array
if keyboard[i].count(bb) : # To check if an element is in the list or NOT
idx = keyboard[i].index(bb) # If it is in, then find its index
print(keyboard[i][idx]) # Print out the letter in myname by index
myname = "Max"
myname = myname.upper()
print("My name is : ")
lookup_keyboard(myname)
print("\n")
mybirthday= "2006.1.25"
mybirthday= mybirthday.upper()
print("My birthday is : ")
lookup_keyboard(mybirthday)
print("\n")
mymonth = "January"
mymonth= mymonth.upper()
print("The month of my birthday is : ")
lookup_keyboard(mymonth)
print("\n")
myage = "16"
myage = myage.upper()
print("My age is : ")
lookup_keyboard(myage)
print("\n")