Assignment 2: Python Using Lists and While-loop
I need to do a assignment and my goal is to look like this
What is your favorite fruit? Banana
What is your favorite fruit? Apple
What is your favorite fruit? Orange
What is your favorite fruit? (empty)
You have 3 fruits in the basket: Banana, Apple, and Orange.
So this is how I will do it with the following plan:
-
To take an input from user we can use the input() function
-
I am going to make a empty list called fruit and then I are going to use the function called append() or insert()
-
So if we want to do that we could use for loop but in this case, I am going to use a new loop called while-loop(When a while loop condition is true the loop keeps on going if false executes the program).
The first step is to make an empty list called fruit after you have made it it will look like this fruit = [] then make a variable and use the function input and what input does is that it takes the input from the user so write the message to the user and make sure to add a space between the ? to make it neater.
#empty list to store fruit item
fruit_list = []
fruit = input("What is your favourite fruit? ")
so when your done we are going to use while loop we type while the variable in this case which is fruit and then we have to make a condition which my condition is that if you enter a empty string it will terminate the code so we do != because it means not equal.
while fruit != "":
When your done we are going to do fruit_list.append(fruit) so basically it means the fruit that the user input will go to the while loop and it will verify the input if it is not equal to the condition then it will go to fruit_list.append(fruit) and the append will place the input into the fruit_list.
fruit_list.append(fruit)
This step is easy all you have to do is to do another input that could be the same message or a different one, but if your wondering why we have another input its because it will just stop the program.
fruit = input("Enter another fruit name or empty space to exit: ")
The last and final step you’ve been waiting for we are going to show a message that says you have (the number of fruit you typed in)in you fruit list: a, b, c, d and e. so how you do it is you use the function print then the message then you use len and the fruit list to count how many fruits there are and then we are going to exclude the last item that the user typed because we can then add and and a fullstop and heres how you do it.
print(f'You have { len(fruit_list) } fruit in list: ')
print(f"{ ', '.join(fruit_list[0:-1]) }, and { fruit_list[-1] }.")
My code:
# empty list to store fruit item
fruit_list = []
fruit = input("What is your favourite fruit? ")
while fruit != "":
fruit_list.append(fruit)
fruit = input("Enter another fruit name or empty space to exit: ")
print(f'You have { len(fruit_list) } fruit in list: ')
print(f"{ ', '.join(fruit_list[0:-1]) }, and { fruit_list[-1] }.")
output
Enter your favourite fruit, or empty space when you finish.
What is your favourite fruit? apple
Enter another fruit name: banana
Enter another fruit name: kiwi
Enter another fruit name: rockmelon
Enter another fruit name: strawberry
Enter another fruit name:
You have 5 fruits in list: apple, banana, kiwi, rockmelon, and strawberry.