Solution:
Create a file named convert_days.py.
On the first line, let's declare the user input. It's an integer, so we cast the string we get from the input function:
days = int(input("Number of days:"))
We can then calculate the number of years in that set of days. We floor divide to get an integer:
years = days // 365
Next, we convert the remaining days that weren't converted to years into weeks:
weeks = (days % 365) // 7
Then, we get any remaining days that weren't converted to weeks:
days = days - ((years * 365) + (weeks * 7))
Finally, we'll print everything out:
print("Years:", years) print("Weeks:", weeks) print("Days:", days)
We can then save and run the script by using the python convert_days.py command.
Solution:
Create a file named convert_to_uppercase.py.
On the first line, we'll request the user for the string to convert:
string = input("String to convert: ")
On the next line, we'll request the number of last letters to convert:
n = int(input("How many last letters should be converted? "))
Next, we'll get the first part of the string:
# First part of the string start = string[:len(string) - n]
Then, we'll get the last part of the string, that is, the one we'll be converting:
# last part of the string that we're converting. end = string[len(string) - n:]
Then, we will concatenate the first and last part back together with the last substring transformed:
print(start + end.upper())
Finally, we can run the script with the python convert_to_uppercase.py command.
Solution:
Create a file named count_occurrences.py.
We'll take in the user inputs for the sentence and the query:
sentence = input("Sentence: ") query = input("Word to look for in sentence: ")
Next, we'll sanitize and format our inputs by removing the whitespace and converting them to lowercase:
# sanitize our inputs sentence = sentence.lower().strip() query = query.lower().strip()
We'll count the occurrences of the substring by using the str.count() method:
num_occurrences = sentence.count(query)
Then, we will print the results:
print(f"There are {num_occurrences} occurrences of '{query}' in the sentence.")
You can run the script by using the python count_occurrences.py command.
Solution:
Create a file named get_first_n_elements.py.
On the first line, we'll create the array:
array = [55, 12, 37, 831, 57, 16, 93, 44, 22]
Next, we'll print the array out and fetch user input for the number of elements to fetch from the array:
print("Array: ", array) n = int(input("Number of elements to fetch from array: "))
Finally, we'll print out the slice of the array from the first element to the nth element:
print(array[0: n])
Then, we'll run the script by using the python get_first_n_elements.py command.
Solutions:
The code block with the missing Boolean operator is added:
n = 124 if n % 2 == 0: print("Even")
The code block with the missing Boolean operator is added:
age = 25 if age >= 18: print("Here is your legal pass.")
The code block with the missing Boolean operator is added:
letter = "b" if letter not in ["a", "e", "i", "o", "u"]: print(f"'{letter}' is not a vowel.")