【问题标题】:Python Sentinel controlled loopPython Sentinel 控制循环
【发布时间】:2014-07-07 21:35:55
【问题描述】:

我想知道是否有人可以帮助我指出正确的方向!我是初学者,我完全迷路了。我正在尝试创建一个 Sentinel 控制循环,要求用户“输入支票金额”,然后询问“这张支票有多少顾客”。在它询问用户然后输入它直到他们输入-1。

一旦用户完成输入,假设计算每张支票的总、小费和税款,8 位以下的顾客收取 18% 的小费,9 岁以上的顾客收取 20% 的小费,税率为 8%。

然后它应该加总总数。 例如:支票 1 = 100$ 检查 2 = 300 检查 3 = 20 总支票 = $420 我不是要求有人为我做这件事,但只要你能指出我正确的方向,这就是我到目前为止所拥有的一切,我被困住了。

截至目前,代码很糟糕,并且无法正常工作。 我在 Raptor 中完成了它,它运行良好我只是不知道如何将它转换为 python

sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0
sum5 = 0
check = 0
print ("Enter -1 when you are done")



check = int(input('Enter the amount of the check:'))
while check !=(-1):
    patron = int(input('Enter the amount of patrons for this check.'))
    check = int(input('Enter the amount of the check:'))

tip = 0
tax = 0


if patron <= 8:
    tip = (check * .18)
elif patron >= 9:
    tip = (check * .20)

total = check + tax + tip
sum1 = sum1 + check
sum2 = sum2 + tip
sum3 = sum3 + patron
sum4 = sum4 + tax
sum5 = sum5 + total

print ("Grand totals:")
print ("Total input check = $" + str(sum1))
print ("Total number of patrons = " + str(sum3))
print ("Total Tip = $" +str(sum2))
print ("Total Tax = $" +str(sum4))
print ("Total Bill = $" +str(sum5))

【问题讨论】:

标签: python loops if-statement while-loop statements


【解决方案1】:

您的代码运行良好,但您有一些逻辑问题。

您似乎打算同时处理多项检查。您可能希望为此使用一个列表,并将支票和赞助人附加到它直到check-1(并且不要附加最后一组价值!)。

我认为您遇到的真正问题是要离开循环,check 必须等于 -1

如果您再往下一点,您将继续使用check,我们现在知道它是-1,不管循环中之前发生了什么(check 每次都会被覆盖)。

当你到达这些线路时,你就会遇到一个真正的问题:

if patron <= 8:
    tip = (check * .18)
elif patron >= 9:
    tip = (check * .20)

# This is the same, we know check == -1

if patron <= 8:
    tip = (-1 * .18)
elif patron >= 9:
    tip = (-1 * .20)

此时您可能无法对您的程序做任何有趣的事情。

编辑:更多帮助

这是我所说的附加到列表的示例:

checks = []
while True:
    patron = int(input('Enter the amount of patrons for this check.'))
    check = int(input('Enter the amount of the check:'))
    # here's our sentinal
    if check == -1:
        break
    checks.append((patron, check))
print(checks)
# do something interesting with checks...

编辑:处理美分

现在您正在将输入解析为 int。没关系,除了"3.10" 的输入将被截断为3。可能不是你想要的。

浮动可能是一个解决方案,但可能会带来其他问题。我建议在内部处理美分。您可能会假设输入字符串是 $(或 € 或其他)。要获得美分,只需乘以 100 ($3.00 == 300¢)。然后在内部你可以继续使用ints。

【讨论】:

  • 您是否尝试过将输入从 3.55 转换为 int?
  • 谢谢!很抱歉,自从我收到这个消息后,我看到你的帖子我一直在努力解决这个问题:c.
  • 拥有 if 赞助人
  • 不,我认为检查顾客数量本身并没有什么坏处。在这些 cmets 中处理您的代码非常棘手。您确实需要弄清楚如何处理多条数据,您可以按照我的示例构建一个列表,然后使用 for 遍历该列表以找到您的总数。继续努力……
【解决方案2】:

这个程序应该可以帮助您入门。如果您需要帮助,请务必使用答案下方的 cmets。

def main():
    amounts, patrons = [], []
    print('Enter a negative number when you are done.')
    while True:
        amount = get_int('Enter the amount of the check: ')
        if amount < 0:
            break
        amounts.append(amount)
        patrons.append(get_int('Enter the number of patrons: '))
    tips, taxs = [], []
    for count, (amount, patron) in enumerate(zip(amounts, patrons), 1):
        tips.append(amount * (.20 if patron > 8 else .18))
        taxs.append(amount * .08)
        print('Event', count)
        print('=' * 40)
        print('  Amount:', amount)
        print('  Patron:', patron)
        print('  Tip:   ', tips[-1])
        print('  Tax:   ', taxs[-1])
        print()
    print('Grand Totals:')
    print('  Total amount:', sum(amounts))
    print('  Total patron:', sum(patrons))
    print('  Total tip:   ', sum(tips))
    print('  Total tax:   ', sum(taxs))
    print('  Total bill:  ', sum(amounts + tips + taxs))

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except (ValueError, EOFError):
            print('Please enter a number.')

if __name__ == '__main__':
    main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2013-03-20
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    相关资源
    最近更新 更多