Info Db

Added some stuff to the Info Db dictionary.

InfoDb = []

# InfoDB is a data structure with expected Keys and Values

# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
    "FirstName": "John",
    "LastName": "Mortensen",
    "DOB": "October 21",
    "Residence": "San Diego",
    "Email": "jmortensen@powayusd.com",
    "Owns_Cars": ["2015-Fusion", "2011-Ranger", "2003-Excursion", "1997-F350", "1969-Cadillac"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Sunny",
    "LastName": "Naidu",
    "DOB": "August 2",
    "Residence": "Temecula",
    "Email": "snaidu@powayusd.com",
    "Owns_Cars": ["4Runner"]
})
#added lukes info
InfoDb.append({
    "FirstName": "Luke",
    "LastName": "Angelini",
    "DOB": "July 29",
    "Residence": "4S Ranch",
    "Email": "ltangelini@gmail.com",
    "Owns_Cars": ["None"]
})

# Allow input
InfoDb.append({
    "FirstName": input(),
    "LastName": input(),
    "DOB": input(),
    "Residence": input(),
    "Email": input(),
    "Owns_Cars": input()
})


# Print the data structure
print(InfoDb)
[{'FirstName': 'John', 'LastName': 'Mortensen', 'DOB': 'October 21', 'Residence': 'San Diego', 'Email': 'jmortensen@powayusd.com', 'Owns_Cars': ['2015-Fusion', '2011-Ranger', '2003-Excursion', '1997-F350', '1969-Cadillac']}, {'FirstName': 'Sunny', 'LastName': 'Naidu', 'DOB': 'August 2', 'Residence': 'Temecula', 'Email': 'snaidu@powayusd.com', 'Owns_Cars': ['4Runner']}, {'FirstName': 'Luke', 'LastName': 'Angelini', 'DOB': 'July 29', 'Residence': '4S Ranch', 'Email': 'ltangelini@gmail.com', 'Owns_Cars': ['None']}, {'FirstName': 'John', 'LastName': 'Smith', 'DOB': 'July 4 ', 'Residence': 'New York', 'Email': 'Johnistheman@gmail.com', 'Owns_Cars': 'Ford F150'}]

For Loops With Index

Here is a basic loop using a list of random items and iterating that list through a for loop. Each item is printed as an instance of the variable i in the list below.

dog_list = ["German Shephard", "Labrador Retriever", "Great Dane", "Chihuahau", "Poodle", "Pitbull"]

# The i variable represents a value in the index dog_list. The for loop prints until the list is complete.
for i in dog_list:
    print(i)
German Shephard
Labrador Retriever
Great Dane
Chihuahau
Poodle
Pitbull

Reversing Text

Data can be output in reverse order. The [::-1] code can be applied to a string in order to reverse it.

def backward(p):
    print(p[::-1])
    print(p)
    return p[::-1] == p

word = input("Enter a word: ")
# A word is collected with the input function and then put into the backward function below
# .casefold() can be put after a variable to convert it to all lowercase
if backward(word.casefold()) is True:
# The p[::-1] reverses the text of the input word and the rest of the function prints the word and returns the word as equal to p
    print("{0} is the same forwards and backwards".format(word))
else:
    print("{0} is not spelled the same forwards and backwards".format(word))
god
dog
dog is not spelled the same forwards and backwards

Simple While Loop

By using a while loop you can iterate through a loop until a certain condition is met. Below is an example created by counting up from 1 to 10.

i = 1
while i <=10:
    print(i)
    i += 1
# The i variable represents the number and each time the number is printed the value i is increased.

print ("Done with loop.")
1
2
3
4
5
6
7
8
9
10
Done with loop.

Adding to Dictionary

You can also add new keys to a dictionary. In this example I created a dictionary with colors that each have objects assigned to them.

New_Dict = {
    "Red": "Fire",
    "Blue": "Water",
    "Green": "Grass"
}
print(New_Dict)

# A New key can be added to the dictionary
New_Dict["Yellow"] = "Lightning"
print(New_Dict)
{'Red': 'Fire', 'Blue': 'Water', 'Green': 'Grass'}
{'Red': 'Fire', 'Blue': 'Water', 'Green': 'Grass', 'Yellow': 'Lightning'}

New Python Quiz

This quiz is created by using a variable, word, which the user inputs into the quiz parameter, prompt, which is then checked for each key in the dictionary to see if it is correct or not.

def Python_Quiz(prompt): 
    global word 
    print ("Question: " + prompt) 
    word = input()
    return word

score = 0


My_Quiz = [{
    "What function is used to output text?":"print",
    "What keyword is used to define a function?":"def",
    "What command allows the use of previously developed functions?":"import",
    "What function is used to collect user input?":"input"
}] #

for dict in My_Quiz: 
    for questions, answers in dict.items(): 
        Python_Quiz(questions) 
        if word == answers: 
            print("Correct answer")
            score += 1  
        else:
            print("Incorrect answer")

print("You got" + str(score) + "/4 correct.")
Question: What function is used to output text?
Answer is incorrect
Question: What keyword is used to define a function?
Answer is incorrect
Question: What command allows the use of previously developed functions?
Answer is incorrect
Question: What function is used to collect user input?
Answer is incorrect
You got0/4 correct.