我陷入了困境

时间:2014-01-28 04:42:46

标签: python

我刚开始编程大约一周前,这对我来说仍然有点新鲜。 每当我介绍一个有效的字母:B,D或W时,程序就会起作用,如果那时我会引入一个不正确的字母,它会要求输入另一个字母,如果我把B,D,W或Q放入,它会再次起作用。 但是,如果我首先引入一个不正确的值,比如Z,然后我给它一个B,那么它将不起作用,它会说它仍然不正确。

提前谢谢!!

#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.')

1 个答案:

答案 0 :(得分:3)

您需要移动此行

correct_code = user_input == 'B' or user_input == 'D' or user_input == 'W'
while循环中

,以便在您检查之前每次都重新计算correct_code的值。就目前而言,correct_code仅在第一次输入后计算一次。