设置sentinel值并使用sentinel值停止循环

时间:2018-03-01 02:25:38

标签: python loops sentinel accumulator

我已经计算出总数并且它们正在运行。一旦输入完成,我就无法让程序停止。它只是保持循环。我已将哨兵设为等于“完成”。有人可以帮忙吗?

# SuperMarket.py - This program creates a report that lists weekly hours worked 
# by employees of a supermarket. The report lists total hours for 
# each day of one week. 
# Input:  Interactive
# Output: Report. 
# Declare variables.
HEAD1 = "WEEKLY HOURS WORKED"
DAY_FOOTER = "Day Total "
SENTINEL = "done"   # Named constant for sentinel value
hoursWorked = 0     # Current record hours
hoursTotal = 0      # Hours total for a day
prevDay = ""        # Previous day of week
notDone = True      # loop control
# Print two blank lines.
print("\n\n")
# Print heading.
print("\t" + HEAD1)
# Print two blank lines.
print("\n\n")

# Read first record 
dayOfWeek = input("Enter day of week or done to quit: ")
if dayOfWeek  == SENTINEL:
    notDone = False
else:
    prevDay = dayOfWeek
    hoursWorked = input("Enter hours worked:")
    while notDone == True:
        dayOfWeek = input("Enter day of week or done to quit: ")
        hoursTotal = hoursTotal + int(hoursWorked)
        if prevDay != dayOfWeek:
            print("\t" + DAY_FOOTER + str(hoursTotal))
            prevDay = dayOfWeek #Include work done in the dayChange()function
            hoursTotal = 0
        hoursWorked = input("Enter hours worked:")

2 个答案:

答案 0 :(得分:0)

您的代码不会仅添加第一条记录上插入的hoursWorked。剩余的天数/输入(即在while循环内部)正常添加。如果这是您的问题,只需将最后几小时工作(在循环内)更改为:

hoursWorked = int(hoursWorked) + int(input("Enter hours worked:"))

答案 1 :(得分:0)

我想出了一切。我不得不添加另一个if语句来使循环在while语句中中断!

var returnRouter = function (client) {

    // do logging
    client.on('save-message', function (socket) { 
        console.log("heheyy");
    });

    ...
};