0

The for loop is working eventhough the provided value is in the list

Tried to run the code in different IDEs. but code did not work in any of these environments

#Check whether the given car is in stock in showroom
carsInShowroom = ["baleno", "swift", "wagonr", "800", "s-cross", "alto", "dezire", "ciaz"]

print("Please enter a car of your choice sir:")

carCustomer = input()

carWanted = carCustomer.lower()

for i in carsInShowroom:
    if i is carWanted:
        print("Sir we do have the Car")
        break
else:
    print("Sorry Sir we do not currently have that model")

Only else block running. When I enter wagonr, the output says "Sorry Sir we do not currently have that model"

shaik moeed
  • 5,300
  • 1
  • 18
  • 54

1 Answers1

0

Change this,

if i is carWanted: # `is` will return True, if 

to

if i == carWanted.strip(): # strip for remove spaces

Why?

  • is is for reference equality.
  • == is for value equality.

*Note: Your input should be wagonr not wagon r

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
  • Thank you so much the code is working. But I did not understand why "if i is carWanted" is not working. could you please tell me why this is happening – Dood2112 Oct 12 '19 at 11:02
  • `is` will return True if two variables point to the same object, `==` if the objects referred to by the variables are equal. Read more [here](https://stackoverflow.com/a/133024/8353711) . In your case, you need to use `==` as you are comparing value of object. – shaik moeed Oct 12 '19 at 11:09
  • Yes your answers indeed resolved my questions... it solved my issue... thank u – Dood2112 Oct 14 '19 at 14:31
  • @Dood2112 Don't forget to accept and upvote this as an answer, if it was helpful and solves your issue. – shaik moeed Aug 24 '23 at 04:06