I had an assignment that I was given for Python. What I have to do is to write a program in Python that asks a user to enter their favourite fruit, store each fruit in a list. At the end of the program, print out the total number of fruits, and also print all the fruits.
This is what I’m expected:
What is your favourite fruit? Banana (the fruit user put)
What is your favourite fruit? Apple (the fruit user put)
What is your favourite fruit? Orange (the fruit user put)
What is your favourite fruit? (empty) (the user didn't write anything)
You have 3 fruits in the basket: Banana, Apple, and Orange.
So how do I do this?
Before I start, I need to make a plan. And here is my plan:
-
Make an empty list so that I can use append() or insert() although today, I’ll be using append() since it’s easier.
-
After that, were going to need to take an input from user so I’ll be using the input() function.
-
For the second last final step, I’m going to use while loop.
-
For the final step, I’m going to print out the last part You have 3 fruits in the basket: Banana, Apple, and Orange. using the function print().
-
This is the very final step. I’m going to put things together.
Step 1. Make an empty list:
To make my empty list, I’m going to make a variable called fruit_basket. At the end of my python program, I’m going to print all the items in the list.
What I typed:
# empty list
fruit_basket = []
Step 2. Using input:
Before I use the function input(), I’m going to make another variable called fruit so that the input is assigned to the variable called fruit. In the function input(), I’m going to write the asked question: What is your favourite fruit? Between the question mark and the last quotes, I’m going to add a space because it’ll look neat and the words won’t really be bunched together.
What I told:
fruit = input("What is your favourite fruit? ")
Step 3. Using while loop:
When using while loop, there is a condition that has to be met. When the condition is true, the code in the while-loop is executed. If it is false, the loop will terminate.
So I’m going to make the condition fruit != “” which means when fruit equals nothing (""), the loop terminates because the condition says fruit does not equal nothing, so if fruit equals nothing, then the loop stops. However, if fruit didn’t equal nothing, then the loop continue.
So this is how it looks like:
Reminder: The while loop always end with a colon.
while fruit != "":
Now I’ve finally started the while loop so I haven’t finished yet.
Now I’m going to use the append() so that when user answer the question, the fruit will go into the variable fruit_basket.
So I’ll type:
fruit_basket.append(fruit)
Great, I’ve finished that part.
So for the last part (which isn’t really the last part), I know that after the append, it’ll stop because it can’t continue the loop so I’ll write the same thing I did with fruit.
fruit = input("What is your favourite fruit? ")
4th Step.
Now I’ve got the last part which is:
You have 3 fruits in the basket: Banana, Apple, and Orange.
This last part is for when the loop stops. When it stop, It will print above the last part I showed you. I’m going to use the f string to put things together. The f string is very useful since it’s pretty easy.
I’ll start with the function print. The I’ll add the f string. Like this:
print(f"(something)")
So let’s start with the bit You have () fruits in the basket: To do this, I need to find the length of the fruits. Like how much they put. So I’ll use the function len() to find the length or the total. So I’ll do len(fruit_basket) but inside the {} because that’s what join do. I’ll also use the parameter end=’’ to specify the ending of the print because by default, it’ll print a new line. So I’ll write end=’’. Therefore, the whole thing will print:
print(f"You have {len(fruit_basket)} fruits in the basket: ",end="")
Now I got the first bit done. Now to the very last bit.
For the last bit, I need to print out the fruits user answered. I’ll start with the function print() like the other bit and I’ll also use the same f string. After the f string, I’ll do the {}.
Then I’ll type:
{ ','.join(fruit_basket:[-1]) }
So that each item is seperated by a comma except for the last item. The [:-1] means everything is printed except for the last item. I did this because I need to add and and I also need to add a full stop at the end of the of the line. to do this, after writing the thing I typed, I’,, add a comma and than do the {}. And inside it, I’ll write:
, and {fruit_basket[-1]}.")
What I typed above was that instead of just a comma between the second last and last items, I’ll put and too to seperate it because it looks nicer like that. After the last {}, I’ll finish the line by adding the full stop. And to finish the print function, I’ll add the last parenthesis.
So this would be how I typed it:
print(f"{ ', '.join(fruit_basket[:-1]) }, and {fruit_basket[-1]}.")
Step 5. Finishing up:
Now that I’ve got all the pieces, I’m going to put this together.
So then this would be how I typed it:
fruit_basket = []
fruit = input("What is your favourite fruit? ")
while fruit != "":
fruit_basket.append(fruit)
fruit = input("What is your favorite fruit? ")
print(f"You have {len(fruit_basket)} fruits in the basket: ", end="")
print(f"{ ', '.join(fruit_basket[:-1]) }, and {fruit_basket[-1]}.")
And what it output:
What is your favourite fruit? Banana (the fruit user put)
What is your favourite fruit? Apple (the fruit user put)
What is your favourite fruit? Orange (the fruit user put)
What is your favourite fruit? (empty) (the user didn't write anything)
You have 3 fruits in the basket: Banana, Apple, and Orange.
Now that I have finished my assignment. I guess that’s all for today.
Thanks for reading!