A6 - F-Strings and Extensions
There is one mandatory part of this assignment, then it moves into extensions for those done early who would like to try them.
In the Python Manual, page 7 goes more into strings. The excerpt relevent to us, covering f-strings, is as follows:
F-Strings - Python Manual Except
A Python f-string can be used to more easily insert data into a string. The f-string must have the letter f prior to the starting quotes, which will allow data to be inserted within curly braces:
name = input("Enter your name:")
age = int(input("Enter your age"))
greeting = f"Good morning {name}. In five years, you'll be {age+5} years old."
# Or, more commonly, print the f-string directly:
print(f"Good morning {name}. In five years, you'll be {age+5} years old.")
The data inside the curly braces can contain variables, function calls, expressions, etc.
Here is another example, where I've modified the sample Madlib program from assignment #2 to make use of f-strings:
name = input("Enter a name: ")
animal = input("Enter an animal: ")
colour = input("Enter a colour: ")
noun = input("Enter a noun: ")
print(f"{name} had a little {animal}")
print(f"whose fleece was {colour} as {noun}.")
Most people find f-strings cleaner and easier to use than the method we were using earlier, and you are welcome to use this technique from now on.
Your Task
Create a file called a6.py.
Paste in your Madlib from assignment #2, and then modify the print statements so that they use f-strings
Extensions
These are optional for those who have completed the rest of the unit with extra time. You may complete any or all of these, or try writing a program of your choice using the skills we've learned so far.
Extension A - Integer Division and Mod
Write a program that makes use of the // and % operators.
We've already used the % (mod) operator.
The // operator performs integer division, meaning it will divide the two numbers and round down.
Here is an example of a simple program that uses both of these:
num_slices = int(input("How many pizza slices do you have? "))
slices_per_pizza = int(input("How many slices are in a pizza? "))
num_pizzas = num_slices // slices_per_pizza
extra_slices = num_slices % slices_per_pizza
print(f"You have enough slices for {num_pizzas} full pizzas, plus {extra_slices} additional slices.")
If you have trouble thinking of your own program that would use these two operators, consider writing a program that would ask the user for a two digit number.
You will then output both the tens digit and the ones digit in separate print statements.
You could extend this further by asking for a three digit number, and outputing all three digits in separate print statements.
Extension B - Multi-line strings
Have a look at how to do multi-line strings on page 7 of the Python Manual.
Create a program that combines multi-line strings with f-strings. It could be another Madlib type program, or something else where values get inserted into multiple lines of text.
Extension C - Choose Your Own Adventure
Create a "Choose Your Own Adventure" style story. Here is a short example:
ans1 = input("You are walking home and discover a new path in the woods. Do you take the path?")
if ans1 == "no":
print("You finish walking home, and never knowing where that path may have led")
exit()
ans2 = input("You walk a short way down the path a hear a strange noise. Do you continue?")
if ans2 == "no":
ans3 = input("The sound appears to be getting closer. Start running?")
if ans3 == "yes":
print("The source of the sounds catches up with you. It was only a neighbourhood dog. You are safe.")
exit()
else:
print("Whatever was making the sound loses interest in you. You never find out what it was.")
else:
print("You continue to find out that the sound was the neigbour's dog. You are safe.")
Notice that this contains nested if statements - basically if statements within other if statements.
Feel free to also use this technique, and go as complex as you wish!