-1

I have a simple code which has a variable set to 0 in the beginning and then adds a number based on an input from a list. But whenever the addition is done, the output is still 0. I added a print after every statement to figure out the problem but it always outputs 0. I'm extremely inexperienced so I do apologize if the fix is simple. Here is the code:

HomeStartCodes = ["C1", "C2", "C3", "C4", "C5"]
HomeStartPrice  = [1.5, 3.0, 4.5, 6.0, 8.0]
TotalPrice = 0.0
        while True:
            CCode = str(input("Please enter the first code of your journey "))
            if (CCode not in HomeStartCodes):
                print("Invalid, please enter C1. C2, C3, C4  or C5")
                continue
            else:
                break
        if CCode is str("C1"):
            TotalPrice = TotalPrice + HomeStartPrice[0]
        if CCode is str("C2"):
            TotalPrice = TotalPrice + HomeStartPrice[1]
        if CCode is str("C3"):
            TotalPrice = TotalPrice + HomeStartPrice[2]
        if CCode is str("C4"):
            TotalPrice = TotalPrice + HomeStartPrice[3]
        if CCode is str("C5"):
            TotalPrice = TotalPrice + HomeStartPrice[4]
        print (TotalPrice)  

Output:

Please enter the first code of your journey C1
0.0
Ali Hosam
  • 21
  • 3

2 Answers2

0

Because you're using is (which is strict identity comparison, which will fail with user-entered strings).

Use == (for any and all comparisons, really).

HomeStartCodes = ["C1", "C2", "C3", "C4", "C5"]
HomeStartPrice  = [1.5, 3.0, 4.5, 6.0, 8.0]
TotalPrice = 0.0
while True:
    CCode = str(input("Please enter the first code of your journey "))
    if (CCode not in HomeStartCodes):
        print("Invalid, please enter C1. C2, C3, C4  or C5")
        continue
    else:
        break
    if CCode == "C1":
        TotalPrice = TotalPrice + HomeStartPrice[1]
    if CCode == "C2":
        TotalPrice = TotalPrice + HomeStartPrice[2]
    if CCode == "C3":
        TotalPrice = TotalPrice + HomeStartPrice[3]
    if CCode == "C4":
        TotalPrice = TotalPrice + HomeStartPrice[4]
    if CCode == "C5":
        TotalPrice = TotalPrice + HomeStartPrice[5]
    print (TotalPrice)

Furthermore, you can simplify your code to not do those comparisons at all by using a dictionary to map your codes to prices:

HomeStartCodes = {
    "C1": 1.5,
    "C2": 3.0,
    "C3": 4.5,
    "C4": 6.0,
    "C5": 8.0,
}
TotalPrice = 0.0
while True:
    CCode = str(input("Please enter the first code of your journey "))
    value = HomeStartCodes.get(CCode)
    if not value:
        print("Invalid")
        continue
    TotalPrice += value
    print(TotalPrice)
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Yesss, this fixed it thank you so much, I will use the dictionary as well, thank you again – Ali Hosam Oct 10 '21 at 19:27
  • Should be `if value is None:` instead of `if not value:` if OP wants value to be added to `TotalPrice` in the case where it is 0. – Bill Oct 10 '21 at 19:48
  • @Bill That is true, but (a) OP doesn't have zero in the values (b) adding zero wouldn't make a difference (c) I told OP not to use `is`, so that'd be hypocritical :) – AKX Oct 10 '21 at 20:02
  • Ha, good point: (b). Adding zero is the same as not adding zero. – Bill Oct 10 '21 at 21:24
0

The is operator performs identity comparisons, which means you don't match any of the cases. You should be using == for string comparisons. Generally, you should use == for comparisons other than comparing to True, False, or None.

Additionally, a list starts at index 0, so you would be getting the next highest value (or IndexError for C5) after you fix the first issue.

Da Chucky
  • 781
  • 3
  • 13