Alex
Alex

Reputation: 61

I get stuck in a loop

I just started programming about a week ago and all this is still a bit new to me. Whenever I introduce a valid letter: B, D or W, the program will work and if then I introduce an incorrect letter it will ask to input another letter, and if I put B, D, W or Q it will work again. But if I first introduce an incorrect value like Z and then I give it a B, it will not work and it will say its still incorrect.

Thank you in advance!!

#This program will calculate the amount due of a customer after they
#rent a car and they input all the data that is asked.

#The import math is for math.ceil, used to round up the days to weeks.
import math

user_input = input ('Enter classification code1: ')

correct_code = user_input == 'B' or user_input == 'D' or user_input == 'W'

while (user_input != 'Q') and (user_input != 'q'):
    print ('Customer code:', user_input, '\n')
    if  correct_code :

        #Here starts the first case. Code W:

        if  user_input == 'B':
            print ('Your classification code is: ', user_input)
            days = int ( input ('Number of days: '))
            odometer_start = int ( input ('Odometer reading at the start: '))
            odometer_end = int ( input ('Odometer reading at the end: '))

            #These are the calculations in case the odometer surpasses the limit (999999)

            if (odometer_start <= 999999 and odometer_start >= 900000) and (odometer_end >= 0 and odometer_end <= 100000) :
                odometer_end += 1000000

            else :
                odometer_end += 0

            #These are the calculations for the amount of miles driven and the amount due

            miles_driven_str = (odometer_end - odometer_start) / 10
            miles_driven = float (miles_driven_str)
            amount_due_str = (40.00 * days) + (0.25 * miles_driven)
            amount_due = float (amount_due_str)

            #this is what the customer will see an now will be able to input their data

            print ('\n')
            print ('Customer summary: ', '\n')
            print ('\t', 'Classification code: B')
            print ('\t', 'Rental period (days): ', days)
            print ('\t', 'Odometer reading at start :', odometer_start)
            print ('\t', 'Odometer reading at end :', odometer_end)
            print ('\t', 'Number of miles driven :', round (miles_driven,1))
            print ('\t', 'Amount due: $', round (amount_due,2))
            print ('\n')

        #This is the second case. Code D:

        elif user_input == 'D' :
            print ('Your classification code is: ', user_input)
            days = int ( input ('Number of days: '))
            odometer_start = int ( input ('Odometer reading at the start: '))
            odometer_end = int ( input ('Odometer reading at the end: '))

            #These are the calculations in case the odometer surpasses the limit (999999)

            if (odometer_start <= 999999 and odometer_start >= 900000) and (odometer_end >= 0 and odometer_end <= 100000) :
                odometer_end += 1000000

            else :
                odometer_end += 0

            #These are the calculations for the amount of miles driven and the basic amount
            #that is due (without calculating the extra miles)

            miles_driven_str = (odometer_end - odometer_start) / 10
            miles_driven = float (miles_driven_str)
            amount_due_str = (60.00 * days)
            amount_due = float (amount_due_str)

            #These are the calculations for the amount to be added to the basic amount due
            #This will add the cost of the extra miles if the customer has travelled more
            #miles that the permitted.

            if miles_driven > 100 :
                extra_miles = miles_driven - 100

            else :
                extra_miles = 0

            amount_due_extra = amount_due + (0.25 * extra_miles)

            #this is what the customer will see an now will be able to input their data

            print ('\n')
            print ('Customer summary: ', '\n')
            print ('\t', 'Classification code: D')
            print ('\t', 'Rental period (days): ', days)
            print ('\t', 'Odometer reading at start :', odometer_start)
            print ('\t', 'Odometer reading at end :', odometer_end)
            print ('\t', 'Number of miles driven :', round (miles_driven,1))
            print ('\t', 'Amount due: $', round (amount_due_extra,2))
            print ('\n')

        elif user_input == 'W' :
            print ('Your classification code is: ', user_input)
            days = int ( input ('Number of days: '))
            odometer_start = int ( input ('Odometer reading at the start: '))
            odometer_end = int ( input ('Odometer reading at the end: '))

            #These are the calculations in case the odometer surpasses the limit (999999)

            if (odometer_start <= 999999 and odometer_start >= 900000) and (odometer_end >= 0 and odometer_end <= 100000) :
                odometer_end += 1000000

            else :
                odometer_end +=0

            #These are the calculations for the amount of miles driven and the basic amount
            #that is due (without calculating the extra miles)

            weeks_rented = math.ceil (days / 7)
            miles_driven_str = (odometer_end - odometer_start) / 10
            miles_driven = float (miles_driven_str)
            amount_due_str = (190.00 * weeks_rented)
            amount_due = float (amount_due_str)

            #These are the calculations for the amount to be added to the basic amount due
            #This will add the cost of the extra miles if the customer has travelled more
            #miles that the permitted.

            if miles_driven > (900 * weeks_rented) and miles_driven < (1500 * weeks_rented) :
                amount_due_extra = amount_due + 100.00

            elif miles_driven > (1500 * weeks_rented) :
                extra_miles = miles_driven - (1500 * weeks_rented)
                amount_due_extra = (amount_due + (200.00 * weeks_rented)) + ( 0.25 * extra_miles )

            else :
                amount_due_extra = amount_due + 0

            #this is what the customer will see an now will be able to input their data

            print ('\n')
            print ('Customer summary: ', '\n')
            print ('\t', 'Classification code: W')
            print ('\t', 'Rental period (days): ', days)
            print ('\t', 'Odometer reading at start :', odometer_start)
            print ('\t', 'Odometer reading at end :', odometer_end)
            print ('\t', 'Number of miles driven :', round (miles_driven,1))
            print ('\t', 'Amount due: $', round (amount_due_extra,2))
            print ('\n')

        #This is for when the input is not: B, D, or W.

        else :
            False
            print ('Incorrect classification code.')

    #This is for when the input is not: B, D, or W.

    else:

        print ('Incorrect classification code. Try again')

    user_input = input ('Enter classification code: ')
    print ('\n')

print ('Done.')

Upvotes: 0

Views: 158

Answers (1)

user1864610
user1864610

Reputation:

You need to move this line

correct_code = user_input == 'B' or user_input == 'D' or user_input == 'W'

inside your while loop so that the value of correct_code is recalculated every time, before you check it. As it stands, correct_code is only calculated once, after the first input.

Upvotes: 4

Related Questions