简体   繁体   中英

Python While Loop how to rerun

Im stuck on this python code I dislike loops. How can I rerun the program if the payment is not >= 20? It just keeps looping otherwise, it wont rerun if the payment isn't >= 20

def make_payment(P):
    while True:
            if payment_amount == -1:
                    exit()
            if payment_amount < 20:
                    print("Retry: Payment should not be less than $20")
            if payment_amount > 1000:
                    print("Retry:Payment exceeds $1000")
            if payment_amount >= 20:
                    print("Sucess")
                    break
print("Welcome,Please make a payment")
amount = input("Please enter payment amount: ")
payment_amount = int(amount)
make_payment(payment_amount)

You do not need a loop in this function:

def make_payment(payment_amount):
    if payment_amount == -1:
        exit()
    if payment_amount < 20:
        print("Retry: Payment should not be less than $20")
    if payment_amount > 1000:
        print("Retry:Payment exceeds $1000")
    if payment_amount >= 20:
        print("Sucess")

Maybe what you want is a while before you ask for the payment_amount. And you want this function to return some value to tell the loop to finish when appropriate.

You can try something like this to make some sense out of the loop:

def make_payment(payment_amount):
    if payment_amount < 20:
        print("Retry: Payment should not be less than $20")
    elif payment_amount > 1000:
        print("Retry:Payment exceeds $1000")
    elif payment_amount >= 20:
        print("Sucess")
        return True
    return False

print("Welcome,Please make a payment")
while True:
    amount = input("Please enter payment amount (-1 to exit): ")
    payment_amount = int(amount)
    if payment_amount == -1:
        exit()
    if not make_payment(payment_amount):
        print("Payment was not accepted, please try again")

Loops are very important in programming, so try to make friends with them.

You should make a main method, call it main() . This will continue to ask for the user input, so long as you make the method call. Also, in the function you wrote, you have multiple if statements with no break or return in the statement, meaning it will go through all of the if statements.

Also, you should let the user know that if the payment_amount is -1, the program will exit. Otherwise, how would they know to exit the program?

payment_amount is not declared within the make_payment function, but you make P the input, so you check if P is a certain number (it is the local variable/input).

def make_payment(P):
    if P == -1:
            exit()

    elif P < 20:
            print("Retry: Payment should not be less than $20\n")

    elif P > 1000:
            print("Retry: Payment exceeds $1000\n")

    elif P >= 20:
            print("Success!\n")

def main():
    print("Welcome! Please make a payment. \n")
    while True:
        amount = input("Please enter payment amount (or type -1 to exit): ")
        payment_amount = int(amount)
        make_payment(payment_amount)

main()

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