Is there a way to make two separate lists for direction and street and make two new ones by manipulating them?

35 Views Asked by At

My instructions are: A little kid has been given directions on how to get to school from his house. Unfortunately he lost the paper that tells him how to get home from school. Being that you are such a nice person, you are going to write a program to help him.

Suppose his mother gave him a note that said the following

R

JOHN

L

KING

L

SCHOOL you need to write the following back:

R KING

R JOHN

L HOME

my code to get this output is:

    directions = ['HOME']

while True:
    d = input('DIRECTION (L or R): ').upper()
    s = input('STREET: ').upper()
    directions.append(d)
    if s == 'SCHOOL':
        break
    directions.append(s)

    new_directions = []

for item in reversed(directions):
    if item == 'L':
        new_directions.append('R')
    elif item == 'R':
        new_directions.append('L')
    else:
        new_directions.append(item)


for item in new_directions:
    print(item)

My code is without 4 lists. My instructor told me I need to make two lists from the input first. one that looks like ['R','L','L'] and the other that looks like ['JOHN','KING','SCHOOL']. but how can I store inputs in a list which are entered at different lines from each other? Like put all the directions in one and all the streets in one??? Then I need to manipulate those lists which lead to two new ones/(the directions back). I'm not really sure how to get there...

1

There are 1 best solutions below

0
Ives Furtado On

You could do something like:

directions = []
streets = []

while True:
    
    direction = input("DIRECTION (L or R): ").upper()
    street = input('STREET: ').upper()
    
    directions.append(direction)
    
    if street == "SCHOOL":
        break
    
    streets.append(street)

# changing directions to make the way back
for index in range(3):
    
    if directions[index] == "R":
        directions[index] = "L"
    
    elif directions[index] == "L":
        directions[index] = "R"

streets.insert(0, "HOME") # to add "HOME" as the firts element on the streets list

# now we reverse it both lists
streets.reverse()
directions.reverse()

# and finally, print out the way back
for index in range(3):
    print(directions[index], streets[index])