【问题标题】:How do I exit a while loop?如何退出while循环?
【发布时间】:2020-04-13 04:30:03
【问题描述】:

我正在编写一个程序,提示用户输入一些信息以输出支付金额。显示金额后,程序会询问用户是否要使用 while 循环重复它。在定义计算工资金额的程序之后,有一个 while 循环来重复输入的问题。问题是我找不到退出循环的方法。 这是我目前所拥有的:

def CalPay(hrs,rate):
    print('Please enter number of hours worked for this week:', hrs)
    print('What is hourly rate?', rate)
    try:
        hrs = float(hrs)
    except: 
        print('You entered wrong information for hours.')
        return
    try:
        rate=float(rate)
    except:
        print('You entered wrong rate information.')
        return
    if hrs < 0:
        print('You entered wrong information for hours.')
    elif rate < 0:
        print('You entered wrong rate information.')
    else:
        if hrs > 60:
            pay=((hrs-60)*2*rate)+(20*rate*1.5)+(rate*40)
            print('Your pay for this week is:', '$'+str(pay))
        elif hrs > 40:
            pay=((hrs-40)*1.5*rate)+(rate*40)
            print('Your pay for this week is:', '$'+str(pay))
        else:
            pay=rate*hrs
            print('Your pay for this week is:', '$'+str(pay))
    repeat=input('Do you want another pay calculation?(y or n)')
    while repeat == 'y' or 'Y':
        while True:
            try:
                hrs = float(input('Please enter number of hours worked for this week:'))
            except: 
                print('You entered wrong information for hours.')
                continue
            else:
                break
        while True:
            try:
                rate=float(input('What is hourly rate?'))
            except:
                print('You entered wrong rate information.')
                continue
            else:
                break
        if hrs < 0:
            print('You entered wrong information for hours.')
        elif rate < 0:
            print('You entered wrong rate information.')
        else:
            if hrs > 60:
                pay=((hrs-60)*2*rate)+(20*rate*1.5)+(rate*40)
                print('Your pay for this week is:', '$'+str(pay))
            elif hrs > 40:
                pay=((hrs-40)*1.5*rate)+(rate*40)
                print('Your pay for this week is:', '$'+str(pay))
            else:
                pay=rate*hrs
                print('Your pay for this week is:', '$'+str(pay))
        repeat=input('Do you want another pay calculation?(y or n)')
    print('Good Bye!')

【问题讨论】:

  • 使用break语句
  • 或在函数中使用 return
  • 这能回答你的问题吗? Python Leave Loop Early
  • 我已经尝试过使用 break,但仍然继续。
  • 请添加一个普通的python标签

标签: python


【解决方案1】:

我认为你的问题是,每次计算后它都会问你问题“'你想要另一个支付计算吗?(y或n)'”如果你回答n,仍然执行在循环中。

您在 while 中使用以下条件

while repeat == 'y' or 'Y':    #this is wrong

当您编写上述条件时,它实际上会解析为

while (repeat == 'y') or ('Y'):

这里第一个条件为假,但第二个条件为真。所以执行会在里面进行。

改为使用下面的“in”关键字。

while repeat in ['y', 'Y']:    #I will prefer this.

while repeat == 'y' or repeat=='Y':

while repeat == ('y' or 'Y'):

希望这会对你有所帮助。

【讨论】:

  • 我刚刚尝试了您推荐的方法,并且成功了。你介意解释一下为什么我的状态是错误的吗?我认为这与您推荐的含义相同。
  • 检查我的帖子编辑。如果对您有意义,请将其标记为答案。或提出问题(如果有)。
【解决方案2】:

您已经嵌套了 while 循环。你需要摆脱它们。

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 2015-01-18
    • 2014-11-28
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多