Question 46. create a script that generates 26 txt files.
import string, os
if not os.path.exists("letters"):
os.makedirs("letters")
for letter in string.ascii_lowercase:
with open("letters/" + letter + ".txt", "w") as file:
file.write(letter + "\n")
Question 47. Write a script that extracts letters from the 26 text file and put the letters in a list.
hints:
['a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
import glob
letters = []
file_list = glob.glob("letters/*.txt")
for filename in file_list:
with open(filename, "r") as file:
letters.append(file.read().strip("\n"))
print(letters)
Question 48. Create a script that iterates through text files and checks if strings p, y, t, h, o, or n are found in the content of the text file. If any of those strings is found, append that string to a list.
import glob
letters = []
file_list = glob.iglob("letters/*.txt")
check = "python"
for filename in file_list:
with open(filename,"r") as file:
letter = file.read().strip("\n")
if letter in check:
letters.append(letter)
print(letters)
Explanation:
The glob module here helps to generate a list of text files.
Then we iterate through that list and read each file inside the loop, strip "\n" characters and then check
if the letter extracted from the file is in string "python" and we append that letter if it is.
Question 48.1. The script is supposed to print out letter "e" if the letter is in string "Hello", but it doesn't. Please try to fix the script.
for letter in "Hello":
if letter == "e":
print(letter)
Expected output:
e
for letter in "Hello":
if letter == "l":
print(letter)
Explanation:
Solution was to indent the print statement because you always indent the line under an if statement.
Question 49. The code is supposed to get some input from the user, but instead it produces an error. Please try to understand the error and then fix it.
pass = input("Please enter your password: ")
Note: Please use raw_input instead of input if you are on Python 2. For Python 3 input is fine.
Hint: You cannot use reserved keywords for variable names
pass1 = input("Please enter your password: ")
Explanation:
There was a SyntaxError error because the syntax to name the variable was wrong since pass is a reserved keyword in Python.
However you can solve that by adding a number to the name or simply choosing another name for the variable.
Question 50. The code produces an error. Please understand the error and try to fix it
age = input("What's your age? ")
age_last_year = age - 1
print("Last year you were %s." % age_last_year)
Note: Please use raw_input instead of input if you are on Python 2. For Python 3 input is fine.
Hint 1: The input function always returns a string type.
Hint 2: Convert the input to an integer with int .
Answer 1:
age = int(input("What's your age? "))
age_last_year = age - 1
print("Last year you were %s." % age_last_year)
Explanation 1:
As you can see we applied an int function to the input function. That converts user input to an integer right away.
Answer 2:
age = input("What's your age? ")
age_last_year = int(age) - 1
print("Last year you were %s." % age_last_year)
Explanation 2:
In this alternative solution we applied the int function in the line where the math operation is taking place. This could be useful
if you're intending to use the user input value as a string in other parts of your script,
so you don't want to convert it to an integer directly.
Question 51. The code produces an error. Please understand the error and try to fix it
The code produces an error. Please understand the error and try to fix it
Hint: An open bracket always needs a closing bracket.
print(type("Hey".replace("ey","i")[-1]))
Explanation:
This code produces a SyntaxError: unexpected EOF while parsing which means that Python found an unexpected End Of File while parsing.
The reason of an unexpected end of file is there's a missing closing bracket at the end of the script.
So, adding a closing bracket at the end of the file fixes the code.
Question 52. The code is supposed to ask the user to enter their name and surname and then it prints out those user submitted values. Instead, the code throws a TypeError. Please fix it so that the expected output is printed out.
firstname = input("Enter first name: ")
secondname = input("Enter second name: ")
print("Your first name is %s and your second name is %s" % firstname, secondname)
Expected output:
Your first name is John and your second name is Smith
firstname = input("Enter first name: ")
secondname = input("Enter second name: ")
print("Your first name is %s and your second name is %s" % (firstname, secondname))
Explanation:
Each of the %s placeholders expects one value after % to be replaced with, but you need to pass these values inside a tuple.
So, putting variables firstname and secondname inside a tuple fixes the code.
Question 53. Print out the last name of the second employee.
d = {"employees":[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}],
"owners":[{"firstName": "Jack", "lastName": "Petter"},
{"firstName": "Jessy", "lastName": "Petter"}]}
Expected output:
Smith
print(d['employees'][1]['lastName'])
Explanation:
d['employees'] returns this list:
[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}]
d['employees'][1] returns the second item of the above list:
{"firstName": "Anna", "lastName": "Smith"}
And finally d['employees'][1]['lastName'] returns the value of lastName :
Smith
Question 54. Please update the dictionary by changing the last name of the second employee from Smith to Smooth or to whatever takes your fancy.
d = {"employees":[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}],
"owners":[{"firstName": "Jack", "lastName": "Petter"},
{"firstName": "Jessy", "lastName": "Petter"}]}
Expected output:
d = {"employees":[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smooth"},
{"firstName": "Peter", "lastName": "Jones"}],
"owners":[{"firstName": "Jack", "lastName": "Petter"},
{"firstName": "Jessy", "lastName": "Petter"}]}
d['employees'][1]['lastName'] = "Smooth"
Explanation:
On the left side of the assignment operator we are accessing Smith as we did in the previous exercise.
Then using the assignment operator we simply assign a new string to that item.
Question 55. Please add a new employee to the dictionary.
d = {"employees":[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}],
"owners":[{"firstName": "Jack", "lastName": "Petter"},
{"firstName": "Jessy", "lastName": "Petter"}]}
Expected output:
{'employees': [{'firstName': 'John', 'lastName': 'Doe'},
{'firstName': 'Anna', 'lastName': 'Smith'},
{'firstName': 'Peter', 'lastName': 'Jones'},
{'firstName': 'Albert', 'lastName': 'Bert'}],
'owners': [{'firstName': 'Jack', 'lastName': 'Petter'},
{'firstName': 'Jessy', 'lastName': 'Petter'}]}
d["employees"].append(dict(firstName = "Albert", lastName = "Bert"))
Explanation:
d['employees'] returns this list:
[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}]
Then d["employees"].append(dict(firstName = "Albert", lastName = "Bert")) appends a new item to the list.
This item happens to be a new dictionary.
Question 56. Store the dictionary in a json file.
import json
d = {"employees":[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}],
"owners":[{"firstName": "Jack", "lastName": "Petter"},
{"firstName": "Jessy", "lastName": "Petter"}]}
with open("company1.json", "w") as file:
json.dump(d, file, indent=4, sort_keys=True)
Explanation:
As you can see we created the json file using the standard file handling method, but then we used json.
dump which makes it easy to write the dictionary content to the file.
indent=4 will create 4 white spaces to indent the different levels of the dictionary items and sort_keys=True
tells Python to preserve the order of the dictionary thrown in the file.
Question 57. Please download the file in the attachment and use Python to print out its content.
Expected output:
{'employees': [{'firstName': 'John', 'lastName': 'Doe'},
{'firstName': 'Anna', 'lastName': 'Smith'},
{'firstName': 'Peter', 'lastName': 'Jones'}],
'owners': [{'firstName': 'Jack', 'lastName': 'Petter'},
{'firstName': 'Jessy', 'lastName': 'Petter'}]}
import json
from pprint import pprint
with open("company1.json","r") as file:
d = json.loads(file.read())
print(d)
Explanation:
We're opening the file in read mode and then using json.
loads which gets a string as output and creates a dictionary object out of that.
Question 58. Please download the json file in the attachment and use Python to add a new employee to the content of the file so that the file looks like in the expected output below.
Expected output:
import string
with open("comany1.json", "r+") as file:
d = json.load(file.read())
d["employee"].append(dict(firstName = "Albert", lastName = "Bert"))
lastName = "Bert"))
file.seek(0)
json.dump(d, file, indent=4, sort_keys=True)
file.truncate()
Question 59. Please complete the code so that it prints out the expected output.
a = [1, 2, 3]
Expected output:
Item 1 has index 0
Item 2 has index 1
Item 3 has index 2
Hint 1: Use a for loop.
Hint 2: Iterate through enumerate(a) .
a = [1, 2, 3]
for index, item in enumerate(a):
print("Item %s has index %s" % (item, index))
Explanation:
enumerate(a) creates an enumerate object which yields pairs of indexes and items.
Then we iterate through that object print out the item-index pairs in each iteration together with some other strings.
Question 60. Create a program that prints Hello repeatedly.
while 1 < 2 :
print("Hello")