Lists, Dictionaries, and Iterations
Code for holding key information using keys and values
import random
# variable of type string
name = "Jishnu"
print("Name:", name, type(name))
# variable of type integer
age = 16
print("Age:", age, type(age))
# variable of type float
grade = 11
print("Grade:", grade, type(grade))
# another variable of type string
favorite_color = " Blue"
print("Favorite Color:", favorite_color, type(favorite_color))
print()
# variable of type list (many values in one variable)
langs = ["Python", "HTML", "CSS", "C", "Java", "JavaScript", "C", "R", "C++", "Bash"]
random_lang = random.choice(langs)
print("Random Language:", random_lang)
dream_cars = ["Toyota Supra", "McLaren 750", "Buggati Chiron", "Tesla Roadster"]
random_car = random.choice(dream_cars)
print("Random Dream Car:", random_car)
print()
# variable of type dictionary (a group of keys and values)
person = {
"Name": name,
"Age": age,
"Grade": grade,
"Languages": langs,
"Favorite Color": favorite_color
}
# prints what is in the person dictionary without brackets
print(*[str(k) + ':' + str(v) for k,v in person.items()])
games = ["Valorant", "Apex Legends", "League of Legends", "Fortnite", "Minecraft", "Pokemon", "Zelda:BOTW", "GTA"]
games.reverse()
print('\nReversed List:'),
print(', '.join(games))
random.shuffle(games)
print('\nRandom Order:'),
print(', '.join(games))
People = []
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person and hobbies
People.append({
"FirstName": "Jishnu",
"LastName": "Singiresu",
"DOB": "April 20",
"Email": "broburn123@gmail.com",
"Dream Car": ["McLaren 750"],
"Hobbies": ["Sleeping", "Playing Games", "Hanging Out with Friends"]
})
# Append to List a 2nd Dictionary of key/values
People.append({
"FirstName": "Luka",
"LastName": "Van Den Boomen",
"DOB": "September 19",
"Email": "unknowndutch@gmail.com",
"Dream Car": ["F1 Racing Car"],
"Hobbies": ["Biking","Sleeping", "Playing Video Games"]
})
# Print the data structure
print(People)
def print_data(person): # defines the data being printed
print("Name:", person["FirstName"], person["LastName"]) # Tells the computer to print certain things from the dictionary
print("Birthday:", person["DOB"])
print("Email:", person["Email"])
print("Dream Car:", ", ".join(person["Dream Car"]))
print("Hobbies:", ", ".join(person["Hobbies"]))
print()
print_data(People[0])
print_data(People[1])
def for_loop(): # for every person in the InfoDB, print their data until there is nothing else to print
for person in People:
print_data(person)
def for_loop_with_index(): # prints out data depending on the length of the dictionary it comes from
for p in range(len(People)):
print_data(People[p])
for_loop()
for_loop_with_index()
def while_loop(): # i starts at 0
i = 0
while i < len(People): # while i is less than the length of the dictionary (2), keep printing the people's data
record = People[i]
print_data(record)
i = i + 1
return
while_loop()
def while_loop_reverse():
i = len(People) - 1
while i >= 0:
print_data(People[i])
i = i - 1
print("REVERSED WHILE LOOP\n")
while_loop_reverse()
def recursive(i): # the recursive loop will keep calling itself until it is greater than or equal to the length of the dictionary
if i >= len(People):
return # this is where the code will end
print_data(People[i]) # prints the data and keeps printing until it has reached the length of the dictionary
return recursive(i + 1)
recursive(0)
import getpass
Subjects = ["Math", "U.S. History", "Random"]
Quiz = []
Quiz.append({
"What is 2+2?" : "4",
"What is 20 x 15?" : "300",
})
Quiz.append({
"What is the year U.S declared independence" : "1776",
"Who is the current U.S president?" : "Joe Biden"
})
Quiz.append({
"What color is cheese?" : "Yellow",
})
def question_with_response(prompt):
print("Question " + ": " + prompt)
msg = input()
return msg
def for_loop_index():
scores = [0, 0, 0]
s = 0
for subject in Quiz:
print("\nSubject: " + Subjects[s])
for ques, ans in subject.items():
rsp = question_with_response(ques)
if rsp == ans:
print("*Ding, Ding, Ding*, Correct answer! You gain 1 point!")
scores[s] += 1
else:
print("*Buzzer sounds*, Damn nice try, better luck next time! You gain no points.")
s += 1
print()
print("Correct answers in " + Subjects[0] + ": " + str(scores[0]) + " out of " + str(len(Quiz[0])))
print("Correct answers in " + Subjects[1] + ": " + str(scores[1]) + " out of " + str(len(Quiz[1])))
print("Correct answers in " + Subjects[2] + ": " + str(scores[2]) + " out of " + str(len(Quiz[2])))
return
print('Hello everyone!, ' + getpass.getuser() + " is our participant for today's trivia, Ready to test your wits?")
print("Spelling counts so make sure you check your spelling and capitalize the first letter!")
for_loop_index()