首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Sentinel控制循环

Python Sentinel控制循环
EN

Stack Overflow用户
提问于 2014-07-08 05:35:56
回答 2查看 4.6K关注 0票数 1

我想知道是否有人能帮我指出正确的方向!我是个初学者,完全迷失了方向。我正在尝试制作一个由Sentinel控制的循环,它要求用户“输入检查金额”,然后询问“这次检查有多少顾客”。在它询问之后,用户然后输入它,直到他们键入-1。

一旦用户完成输入,这是应该计算的总,小费,税的每个检查与18%的小费为任何8个顾客和20%的小费为任何超过9和税率为8%。

然后,它应该将总计相加。例如: check 1= 100$ check 2= 300 check 3= 20 Total check= $420我并不是要求别人为我做这件事,但如果你能给我指出正确的方向,这就是我到目前为止所拥有的全部,我被卡住了。

到目前为止,代码是可怕的,并且不能真正工作。我在Raptor中完成了它,它工作得很好,我只是不知道如何将它转换成python

代码语言:javascript
复制
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))
EN

回答 2

Stack Overflow用户

发布于 2014-07-08 05:50:44

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

看起来您计划同时处理多个检查。您可能希望为此使用一个列表,并将支票和顾客附加到该列表中,直到check-1为止(并且不要附加最后一组值!)。

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

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

当你读到这几行的时候,你就会遇到一个真正的问题:

代码语言:javascript
复制
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)

在这一点上,你可能无法对你的程序做任何有趣的事情。

编辑:更多帮助

下面是我所说的添加到列表中的示例:

代码语言:javascript
复制
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。可能不是你想要的。

Float可能是一种解决方案,但也会带来其他问题。我建议在内部处理美分。您可能会假设输入字符串是$(或欧元或其他格式)。要得到美分,只需乘以100 ($3.00 == 300¢)。然后,您可以在内部继续使用int%s。

票数 2
EN

Stack Overflow用户

发布于 2014-07-08 07:20:00

这个程序应该可以让你入门了。如果你需要帮助,一定要使用答案下面的注释。

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24620287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档