简体   繁体   中英

How do I exit a while loop?

I am writing a program that prompts the user to input some information to output the pay amount. After displaying the amount, the program asks the user whether the user wants to repeat it using the while loop. After the definition of the program that calculates the pay amount, there is a while loop to repeat the questions for the inputs. The problem is that I cannot find a way to exit the loop. Here is what I have so far:

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!')

You have nested while loops. You'll need to break out of both of them.

I think your problem is, every time after calculation it is asking you question "'Do you want another pay calculation?(y or n)'" and if you answer n, still execution is going inside loop.

you are using below condition in while

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

when you write above condition it actually resolves to

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

here first condition is false but 2nd is true. So execution will go inside while.

Instead use 'in' keyword as below.

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

or

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

or

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

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM