2

How would i use and if and else statement in a list? Like if i have a premade list then a user inputs their list how would I make it to where it displays we entered the same answer. this is the assignment:

Pick a category and make a list of your five favorite things. Suggested categories include your favorite: actors, books, cars, or something else of your choosing.

Ask the user what their favorites are in your selected category.

By creating a list, a loop, and if statements, print a message that lets the user know if their favorites matches the ones on your list.

Neatly print your entire list to the screen. Be sure to number each item.

Write the pseudocode for this program. Be sure to include any needed input, calculations, and output.

Here is my current code:

def main():

    print("What are your five favorite cars?")

    favoriteCarOne = input("What is your first favorite car?")
    favoriteCarTwo = input("What is your second favorite car?")
    favoriteCarThree = input("What is your third favorite car?")
    favoriteCarFour = input("What is your fourth favorite car?")
    favoriteCarFive = input("What is your fifth favorite car?")

    userCarList = [favoriteCarOne, favoriteCarTwo, favoriteCarThree, favoriteCarFour, favoriteCarFive]
    daltonCarList = ["Shelby", "Dodge HellCat", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]

    for n in range(0, len(userCarList)):
        print(str(n) + " " + userCarList[n])


    daltonLength = len(daltonCarList)
    userLength = len(userCarList)

    if (userCarList == daltonCarList):
        print("Cool! We like the same cars!")
        print

    else:
        print("Those are cool cars!")
        print

    print("These are my favorite cars!")
    print(daltonCarList)
    print


main()
SCB
  • 5,821
  • 1
  • 34
  • 43

2 Answers2

0

List comprehensions are your friend!

matched = [i for i in daltonCarList if i in userCarList]

if matched:
    print "Cool, we liked some of the same cars!"
    print matched

This code will produce a new list containing matches from dalton and user.

If you want to see if both lists are exactly the same, your answer lies here

Matt_G
  • 506
  • 4
  • 14
0

Python can compare two lists just fine:

a = ["Shelby", "Dodge HellCat", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]
b = ["Shelby", "Dodge HellCat", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]

if a == b:
    print("They are the same")
else:
    print("They are different")

# Outputs: They are the same

However this won't work if they aren't in the same order:

a = ["Shelby", "Dodge HellCat", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]
b = ["Dodge HellCat", "Shelby", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]

if a == b:
    print("They are the same")
else:
    print("They are different")

# Outputs: They are different

Based on the requirements, it seems that you need to check that all the elements exist in both lists, but not necessarily in order. The easiest way to do this is probably to use two sets, however it seems that you need to use a loop.

Instead, maybe try to use the not in keyword to check if the car exists. An example of this would be:

for car in their_cars:
    if car not in my_cars:
        print("Oh no, a car that's not in my list")

Warning, this isn't going to be complete. Say someone enters "Shelby" 5 times, it would pass. So you'd need to reverse it and check backwards.

SCB
  • 5,821
  • 1
  • 34
  • 43
  • That makes a lot of sense but I can't use that since my online teacher is strict about only using things in the lesson we learned. The course can be very vague at times. It has gone over loops but not too much depth. – Dalton Scarbrough Feb 12 '18 at 22:35
  • Well `in` can be replicated with another `for` loop: `for my_car in my_cars: if car == my_car: # car is in my_cars`. (If that makes sense spread across a single line). – SCB Feb 12 '18 at 22:46