Home > Software design >  why are all if statements NOT Executed in this code?
why are all if statements NOT Executed in this code?

Time:10-08

This is a part of the program i am writing for a transport system. the scenario is that a journey has three stages: Stage 1 will pick passenger from home and drop at the station. Stage 2 will take passenger from departure station to arrival station. Stage 3 will take passenger from arrival station to the actual destination they want to go to. Each stage has a price and corresponding code which the passenger has to enter when booking a journey. Finally total price has to be calculated for the journey.The relevant code is shown below.

I created 3 lists to store those codes with their prices and take input of the codes from the passenger. I thought that using a for loop to iterate over each item in list I can just compare it against the input by user and then get the corresponding price. But this code does not do what i think it should do. I am going to guess i made a very silly mistake but i just cant figure it out.

What i want from this code is the correct price. but it does not give the correct price. E.g: if i choose 'C1','M1','F1' as input the price should total to 8.75 but it outputs 1.5 instead.

journeyStageOne = [['C1',1.50],['C2',3.00],['C3',4.50],['C4',6.00],['C5',8.00]]
journeyStageTwo = [['M1',5.75],['M2',12.50],['M3',22.25],['M4',34.50],['M5',45.00]]
journeyStageThree = [['F1',1.50],['F2',3.00],['F3',4.50],['F4',6.00],['F5',8.00]]

code1 = input('Enter code for stage 1 of journey: ')
code2 = input('Enter code for stage 2 of journey: ')
code3 = input('Enter code for stage 3 of journey: ')

total = 0
price1 = 0
price2 = 0
price3 = 0

for count in range(5):

    if journeyStageOne[count][0] == code1.upper():
        price1 = journeyStageOne[count][1]
        print('one')

    if journeyStageTwo[count][0] == code2.upper():
        price2 = journeyStageTwo[count][1]
        print('two')

    if journeyStageThree[count][0] == code3.upper():
        price3 = journeyStageThree[count][1]
        print('three')

total = price1 price2 price3
print(total)

*Those print statements inside for loop were used just to check until where the code executes and it only does so until the first if block. Why?

If my approach is wrong please do tell me what i should write instead of all this.

CodePudding user response:

The only change in your code in the third if loop, you wrote

journeyStageTwo[count][0] 

instead of

journeyStageThree[count][0]

That's the resaon you are getting wrong output, I hope you will correct it

CodePudding user response:

You are not able to get price2 while use "journeyStageTwo[count][0] == code1.upper()" - why not code2? The same thing for price3, plus "price3 = journeyStageTwo[count][1]" - why not journeyStageThree?

  • Related