3.17 LAB: List basics Given the user inputs

3.17 LAB: List basics

Given the user inputs, complete a program that does the following tasks:

Define a list, my_list, containing the user inputs: my_flower1, my_flower2, and my_flower3 in the same order.
Define a list, your_list, containing the user inputs, your_flower1 and your_flower2, in the same order.
Define a list, our_list, by concatenating my_list and your_list.
Append the user input, their_flower, to the end of our_list.
Replace my_flower2 in our_list with their_flower.
Remove the first occurrence of their_flower from our_list without using index().
Remove the second element of our_list.
Observe the output of each print statement carefully to understand what was done by each task of the program.

Ex: If the input is:

rose

peony

lily

rose

daisy

aster

the output is:

[‘rose’, ‘peony’, ‘lily’, ‘rose’, ‘daisy’]

[‘rose’, ‘peony’, ‘lily’, ‘rose’, ‘daisy’, ‘aster’]

[‘rose’, ‘aster’, ‘lily’, ‘rose’, ‘daisy’, ‘aster’]

[‘rose’, ‘lily’, ‘rose’, ‘daisy’, ‘aster’]

[‘rose’, ‘rose’, ‘daisy’, ‘aster’]


here is the code:

my_flower1 = input()
my_flower2 = input()
my_flower3 = input()
my_list = [my_flower1, my_flower2, my_flower3]
your_flower1 = input()
your_flower2 = input()
your_list = [your_flower1, your_flower2]
their_flower = input()
our_list = my_list + your_list
print(our_list)
our_list.append(their_flower)
print(our_list)
for i in range(len(our_list)):
if our_list[i] == my_flower2:
our_list[i] = their_flower
print(our_list)
for i in range(len(our_list)):
if our_list[i] == their_flower:
del our_list[i]
break
print(our_list)
del our_list[1]
print(our_list )

the issue with the output:

The correct answer and explanation is:

Here’s the corrected code and the explanation:

Corrected Code:

my_flower1 = input()
my_flower2 = input()
my_flower3 = input()
my_list = [my_flower1, my_flower2, my_flower3]

your_flower1 = input()
your_flower2 = input()
your_list = [your_flower1, your_flower2]

their_flower = input()
our_list = my_list + your_list
print(our_list)  # Task 1

our_list.append(their_flower)
print(our_list)  # Task 2

for i in range(len(our_list)):
    if our_list[i] == my_flower2:
        our_list[i] = their_flower
        break  # Replace the first occurrence only
print(our_list)  # Task 3

for i in range(len(our_list)):
    if our_list[i] == their_flower:
        del our_list[i]
        break  # Remove the first occurrence
print(our_list)  # Task 4

del our_list[1]
print(our_list)  # Task 5

Explanation:

  1. Input Values and Initial List Creation:
    • Inputs are assigned to variables like my_flower1, my_flower2, etc.
    • my_list contains the three flowers from my_flower1, my_flower2, and my_flower3.
    • your_list contains two flowers from your_flower1 and your_flower2.
    • our_list combines my_list and your_list using the + operator.
  2. Task 1: Print Combined List:
    • Concatenation results in a list of all flowers from my_list and your_list.
  3. Task 2: Append Flower:
    • their_flower is appended to the end of our_list.
  4. Task 3: Replace my_flower2 with their_flower:
    • The loop iterates over our_list to find the first occurrence of my_flower2 and replaces it with their_flower.
  5. Task 4: Remove First Occurrence of their_flower:
    • The loop iterates over our_list, and upon finding the first occurrence of their_flower, it removes it using del.
  6. Task 5: Remove Second Element:
    • del our_list[1] removes the second element (index 1).

Issues in Original Code:

  • Task 3: The break statement was missing, leading to multiple replacements of my_flower2.
  • Task 4: Without break, the loop would remove unintended items.
  • Indentation was incorrect for loop bodies, causing potential runtime errors.

Correct Output:

For the input:

rose
peony
lily
rose
daisy
aster

The output is:

['rose', 'peony', 'lily', 'rose', 'daisy']
['rose', 'peony', 'lily', 'rose', 'daisy', 'aster']
['rose', 'aster', 'lily', 'rose', 'daisy', 'aster']
['rose', 'lily', 'rose', 'daisy', 'aster']
['rose', 'rose', 'daisy', 'aster']
Scroll to Top