无法中断无限循环

2024-06-26 02:56:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有很多关于无限循环的帖子,但没有一篇反映了我的特殊困境(它们涉及Java或者与我的代码格式不匹配等等)。我所用的代码实际上是针对像我这样的新生的练习的源代码或“答案代码”,它只提供了“没有正确格式的正确代码”,对于一个独立的学生来说,这可以使事情复杂化,但也提供了一个更有成效的挑战。你知道吗

代码充分利用了“函数”和“从其他函数中调用函数”,因此只剩下很少的“全局代码”,这可能会使事情稍微复杂一些,但希望有经验的程序员不会被这种情况所影响。你知道吗

我认为循环要么是我的“while循环代码缩进”的问题,要么是“while循环条件/计数器代码本身”的问题。循环代码从程序代码的其他部分获取和使用数据,不应该完全排除,但实际上我怀疑问题是缩进或内部循环代码本身的前两个可能问题之一,我已经尝试了“缩进布局”的多种变体,并进行了快速修复(键入错误的语法等)。你知道吗

相关代码可在程序代码末尾找到(程序代码中只有一个“while循环”),它位于代码的“菜单选项”部分,位于“#通过引号循环选择引用相应月份的选项,并将数据存储在摘要字典中”。你知道吗

我包括了两个独立的代码窗口,一个突出显示可疑的“问题代码”,另一个带有“完整程序代码”。任何方面的帮助都将不胜感激。你知道吗

最可能包含错误的代码段

def monthlyReport():
    file = open(QUOTES_TO_DATE_FILE, 'r')

    text = file.read()

    file.close()

    quotes = text.split()

    month = input('Enter month: ')

    summary = {'Lawn':{'Quantity' : 0.0, 'Value' : 0.0}, 'Patio' :{'Quantity' : 0.0, 'Value' : 0.0}, 'Water Feature' :{'Quantity' : 0.0, 'Value' : 0.0}}
# Loop through quotes selecting those referencing the appropriate month and
#store the data in summary dictionary
    index = 0

    while True:

    if quotes[index] == month:

            inputQuotesFromFile2(quotes[index+1])    

            summary['Lawn']['Quantity'] = summary['Lawn']['Quantity'] + quote['Lawn']['Width'] * quote['Lawn']['Length']

            summary['Lawn']['Value'] = summary['Lawn']['Value'] + quote ['Lawn']['Cost']

            summary['Patio']['Quantity'] = summary['Patio']['Quantity'] + quote['Patio']['Width'] * quote['Patio']['Length']

            summary['Patio']['Value'] = summary['Patio']['Value'] + quote['Patio']['Cost']

            summary['Water Feature']['Quantity'] = summary['Water Feature']['Quantity'] + quote['Water Feature']['Quantity']

            summary['Water Feature']['Value'] = summary['Water Feature']['Value'] + quote['Water Feature']['Cost']

            index = index + 2

        if (index >= len(quotes)):

            break

        totalValue = summary['Lawn']['Value'] + summary['Patio']['Value'] + summary['Water Feature']['Value']

        outputSummaryDictionary(summary, month, totalValue)

完整程序代码

# `Dictionary containing time values (mins) per square metre/ per feature
##lawn:20   patio:20  water feature:60
TIME = {'Lawn': 20, 'Patio': 20, 'Water Feature': 60}

# Constant for labour cost
##16.49
LABOUR_COST = 16.49

# Variable for filename of list of quotes made to date
##quotesToDateFile
QUOTES_TO_DATE_FILE = 'quotesToDateFile.txt'



# 'Global variables'

# A dictionary that stores quote data temporarily, contains sub dicts for each
#material type including keys for length, width, cost, time/quantity,cost, time
quote = {'Lawn':{'Length': 0 , 'Width': 0 , 'Cost': 0.0 , 'Time': 0.0},
         'Patio':{'Length': 0 , 'Width': 0, 'Cost': 0.0 , 'Time': 0.0 },
         'Water Feature':{'Quantity': 0 , 'Cost': 0.0 , 'Time': 0.0}}

# A dictionary storing material costs of individual items (can be updated)
materialCost = {'Lawn': 15.5, 'Patio': 20.99, 'Water Feature': 150}


# 'Input'

# Function to input material info defined by a length
##create function with named parameter for 'item'
def inputItemDimensions(item):

    s = 'Enter length of ' + item + ':'

    length = int(input('Enter length of material: '))

    s = 'Enter width of ' + item + ':'

    width = int(input('Enter width of material: '))

    return length, width

# Function to input material info defined by quantity
##create function with named parameter 'item
def inputItemQuantity(item):

    s = 'Enter quantity of ' + item + ':'

    quantity = int(input('Enter quantity of items: '))

    return quantity

# Function for input of area and quantity
def itemInput():

    global quote

    quote['Lawn']['Length'], quote['Lawn']['Width'] = inputItemDimensions('lawn')

    quote['Patio']['Length'], quote['Patio']['Width'] = inputItemDimensions('concrete patio')

    quote['Water Feature']['Quantity'] = inputItemQuantity('water feature')



# 'Cost calculation'

# Function to calculate, output to screen, return the material cost and time
#to install a landscape item installed by length and width
def costCalculation1(num, item, length, width, cost, time):

    print('[{0}]'.format(num))

    print('Length and width of the {0} = {1} x {2}m'.format(item, length, width))

    area = length * width

    print('Total area of {0} = {1:.2f}m^2'.format(item, area))


    print('Cost of {0} per m^2 = £{1:.2f}'.format(item, cost)) 


    totalCost = area * cost 

    print('Total cost of {0} = £{1}\n'.format(item, totalCost))

    totalTime = area * time

    return totalCost, totalTime

# Function to calculate, output to screen and return the material cost and time
#to install a landscape item installed by quantity
def costCalculation2(num, item, quantity, cost, time):

    print('[{0}]'.format(num))

    print('Quantity of {0} = {1} items'.format(item, quantity))

    print('Cost of one {0} = £{1:.2f}'.format(item, cost))

    totalCost = quantity * cost 

    print("Total cost of {0} {1} = £{2}\n".format(quantity, item, totalCost))

    totalTime = quantity * time

    return totalCost, totalTime

# Function to calculate individual costs of items
def calculateItemCosts():

    global quote

    quote['Lawn']['Cost'], quote['Lawn']['Time'] = costCalculation1('1', 'lawn', quote['Lawn']['Length'], quote['Lawn']['Width'], materialCost['Lawn'], TIME['Lawn'])

    quote['Patio']['Cost'], quote['Patio']['Time'] = costCalculation1('2', 'patio', quote['Patio']['Length'], quote['Patio']['Width'], materialCost['Patio'], TIME['Patio'])

    quote['Water Feature']['Cost'], quote['Water Feature']['Time'] = costCalculation2('3', 'water features', quote['Water Feature']['Quantity'], materialCost['Water Feature'], TIME['Water Feature'])


# Function to calculate workimg costs and output them
def workingCost():

    print('Working costs:')

    totalTime = (quote['Lawn']['Time'] + quote['Patio']['Time'] + quote['Water Feature']['Time']) / 60

    labourCost = totalTime * LABOUR_COST

    print('Total time to complete work = {0} mins'.format(totalTime))

    print('Cost of work per hour = £{0}'.format(LABOUR_COST)) 

    print('Total cost of work = £{0}\n'.format(labourCost))

# Calculate total fee payable by customer, output to screen and file
    materialCost = quote['Lawn']['Cost'] + quote['Patio']['Cost'] + quote['Water Feature']['Cost']

    totalCost = materialCost + labourCost

    print('Total cost to pay = £{0}\n'.format(totalCost))


# 'Output functions'

# Output details concerning item
def outputItems():

    outputItems1('1', 'Lawn', quote['Lawn'])

    outputItems1('2', 'Patio', quote['Patio'])

    outputItems2('3', 'Water Feature', quote['Water Feature'])


# Output dimensions and cost for certain item

def outputItems1(num, item, itemDict):

    print('[{0}]'.format(num))

    print('Length of width of {0} = {1}m x {2}m'.format(item, itemDict['Length'], itemDict['Width']))

    print('Total cost of {0} = £{1}'.format(item, itemDict['Cost']))

    print('Time to install {0} = {1}mins\n'.format(item, itemDict['Time'] / 60))

# Output quantity and cost for item

def outputItems2(num, item, itemDict):

    print('[{0}]'.format(num))

    print('Quantity of {0} = {1} items'.format(item, itemDict['Quantity']))

    print('Cost of one {0} = £{1:.2f}'.format(item, itemDict['Cost']))

    print('Time to install {0} = {1:.2f} hours\n'.format(item, itemDict['Time'] / 60))

# Output material cost dictionary
def outputMaterialCostDictionary():

    for key, value in materialCost.items():

        print('{0} = {1}'.format(key, value))

        print('\n')

# Output summary dictionary
def outputSummaryDictionary(summaryD, month, totalV):

    outputSummaryItem1(['Month', month, '', '', ''])

    outputSummaryItem1(['Total', '', 'Total', 'Total', 'Total'])

    outputSummaryItem1(['Working', 'Item', 'Square metre', 'Number', 'Monthly'])

    outputSummaryItem1(['Costs', '', 'Purchased', 'Purchased', 'Value'])  

    outputSummaryItem2('Lawn', summaryD['Lawn'])

    outputSummaryItem2('Patio', summaryD['Patio'])

    outputSummaryItem3('Water Feature', summaryD['Water Feature'])

    outputSummaryItem4(totalV)

# Output summary dictionary item ver 1
def outputSummaryItem1(sList):
    print('|{0:^13}|{1:^13}|{2:^13}|{3:^13}|{4:^13}|'.format(sList[0], sList[1], sList[2], sList[3], sList[4]))

# Output summary dictionary item ver 2
def outputSummaryItem2(name, item):
    print('|{0:^13}|{1:^13}|{2:13.2f}|{3:^13}|{4:13.2f}|'.format('', name, item['Quantity'], '', item['Value']))

# Output summary dictionary item ver 3
def outputSummaryItem3(name, item):
    print('|{0:^13}|{1:^13}|{2:^13}|{3:13.0f}|{4:13.2f}|'.format('', name, '', item['Quantity'], item['Value']))

# Output summary dictionary item ver 4
def outputSummaryItem4(totalValue):
    print('|{0:^13}|{1:^13}|{2:^13}|{3:^13}|{4:13.2f}|'.format('Total', '', '', '', totalValue))




# 'File handling'

# Function to output file
def outputToFile():

    filename = input('Enter file name: ')

    file = open(filename, 'w')

    month = input('Enter month:' )

    print('Filename = {0}....Month = {1}\n'.format(filename, month))

    file.write('{0}\n'.format(month))

    s = '{0} {1} {2} {3}\n'.format(quote['Lawn']['Length'], quote['Lawn']['Width'], quote['Lawn']['Cost'], quote['Lawn']['Time'])
    file.write(s)
    s = '{0} {1} {2} {3}\n'.format(quote['Patio']['Length'], quote['Patio']['Width'], quote['Patio']['Cost'], quote['Patio']['Time'])
    file.write(s)
    s = '{0} {1} {2}\n'.format(quote['Water Feature']['Quantity'], quote['Water Feature']['Cost'], quote['Water Feature']['Time'])
    file.write(s)
    file.close()


# Update quotes to date file 
    file = open(QUOTES_TO_DATE_FILE, 'a')


    s = '{0} {1}\n'.format(month, filename)

    file.write(s)
    file.close()

# Function to input quote from file where file name is not known
def inputQuoteFromFile1():

    filename = input('Enter name for input file: ')

    inputQuoteFromFile2(filename)


# Function to input quote from file when file IS known
def inputQuoteFromFile2(filename):

    file = open(filename, 'r')

    text = file.read()

    list1 = text.split()

    file.close()

# Process the data (ignore first item which is the month)
##declare 'quote' dict as global (this might mean this code is within function)
    global quote

    subDictionary = {'Length' : float(list1[1]), 'Width' : float(list1[2]), 'Cost' : float(list1[3]), 'Time' : float(list1[4])}

    quote['Lawn'] = subDictionary

    subDictionary = {'Length' : float(list1[5]), 'Width' : float(list1[6]), 'Cost' : float(list1[7]), 'Time' : float(list1[8])}

    quote['Patio'] = subDictionary

    subDictionary = {'Quantity' : float(list1[9]), 'Cost' : float(list1[10]), 'Time' : float(list1[11])}

    quote['Water Feature'] = subDictionary

    file.close()



# 'Menu options'

# Function to allow preperation of a new quote
def prepareANewQuote():
    itemInput()

    calculateItemCosts()

    workingCost()

    outputToFile()

# Function to load new material costs
def loadNewMaterialCosts():
    filename = input('Enter filename: ')

    file = open(filename, 'r')

    text = file.read()

    file.close()

    newMaterialCosts = text.split()

# Assign costs to material cost dictionary
    index = 0

    for key in materialCost.keys():

        materialCost['Key'] = float(newMaterialCosts['index'])

        index = index + 1

# Output new material costs # NOTE MAY NEED TO BE INDENTED FURTHER
    outputMaterialCostDictionary()
# Function to view and load existing quote
def viewExistingQuote():
    inputQuoteFromFile1()

    outputItems()

    workingCost()
# Function to generate monthly report summary
def monthlyReport():
    file = open(QUOTES_TO_DATE_FILE, 'r')

    text = file.read()

    file.close()

    quotes = text.split()

    month = input('Enter month: ')

    summary = {'Lawn':{'Quantity' : 0.0, 'Value' : 0.0}, 'Patio' :{'Quantity' : 0.0, 'Value' : 0.0}, 'Water Feature' :{'Quantity' : 0.0, 'Value' : 0.0}}
# Loop through quotes selecting those referencing the appropriate month and
#store the data in summary dictionary
    index = 0

    while True:

        if quotes[index] == month:

            inputQuotesFromFile2(quotes[index+1])    

            summary['Lawn']['Quantity'] = summary['Lawn']['Quantity'] + quote['Lawn']['Width'] * quote['Lawn']['Length']

            summary['Lawn']['Value'] = summary['Lawn']['Value'] + quote ['Lawn']['Cost']

            summary['Patio']['Quantity'] = summary['Patio']['Quantity'] + quote['Patio']['Width'] * quote['Patio']['Length']

            summary['Patio']['Value'] = summary['Patio']['Value'] + quote['Patio']['Cost']

            summary['Water Feature']['Quantity'] = summary['Water Feature']['Quantity'] + quote['Water Feature']['Quantity']

            summary['Water Feature']['Value'] = summary['Water Feature']['Value'] + quote['Water Feature']['Cost']

            index = index + 2

        if (index >= len(quotes)):

            break

        totalValue = summary['Lawn']['Value'] + summary['Patio']['Value'] + summary['Water Feature']['Value']

        outputSummaryDictionary(summary, month, totalValue)         



# 'Main' (initialisation)

# Top level function
def start():
    while True :

        print('Select one of following options')

        print('(1) Prepare new quote')

        print('(2) Load new cost data')

        print('(3) Load and view existing quote')

        print('(4) Generate monthly report summary')

        print('(5) Exit')

        selection = int(input())

        if selection == 1:

            prepareANewQuote()

        elif selection == 2:

            loadNewMaterialCosts()

        elif selection == 3:

            viewExistingQuote()

        elif selection == 4:

            monthlyReport()

        elif selection == 5:

            quit()

        else:

            print('Error unrecognised command')

# Start
start()

Tags: offormatvaluedefsummaryitemfeaturequantity
1条回答
网友
1楼 · 发布于 2024-06-26 02:56:58

如果quotes[index]不等于month,则index永远不会被修改,因此代码将反复检查相同的值,并且永远不会继续。你知道吗

您应该将index的赋值取消一级。但实际上这并不是while循环的适当用法;您应该使用for来迭代quotes

for quote in quotes:

(还要注意,在这段代码中有两个while循环;实际上,far过多地使用了global。)

相关问题 更多 >