Home > Mobile >  Python - trying to work out the correct loop in this method, it works util selecting the payment met
Python - trying to work out the correct loop in this method, it works util selecting the payment met

Time:10-20

So I am trying to code a checkout function, this is how it looks, at the moment it doesn't work when selecting the payment method I think its the loop or that I have used checkout == twice in the function.

 def checkout(self):
      while True:
        if not shopping_cart:
            print("Your cart is empty, please add item to proceed to checkout")

        else:
            checkout = int(input('''Do you want to proceed to checkout?
                                1. Yes
                                2. No '''))
            if checkout == 1:
               checkout = input('''Please select how you would like to pay for your purchase: 
                                1. Gift Voucher
                                2. Credit / Debit Card
                                3. Paypal''')
            if checkout == 2:
               break

            else:
                 if checkout  == 1:
                    input("Please enter your gift voucher code")
                    print("Thank you")

                 elif checkout == 2:
                    input("Please enter your Credit / Debit card information")
                    print("Thank you")

                 elif checkout == 3:
                     input("vffsdfsdfsd")
                     print("TA")

                 else:
                     print("Please select a valid payment method")
                     break

CodePudding user response:

It is because you are using same variable Do you want to proceed to checkout? and Please select how you would like to pay variable try changing variable for payment method type as payment_method

 def checkout(self):
          while True:
            if not shopping_cart:
                print("Your cart is empty, please add item to proceed to checkout")
    
            else:
                checkout = int(input('''Do you want to proceed to checkout?
                                    1. Yes
                                    2. No '''))
                if checkout == 1:
                   payment_method = input('''Please select how you would like to pay for your purchase: 
                                    1. Gift Voucher
                                    2. Credit / Debit Card
                                    3. Paypal''')
                if checkout == 2:
                   break
    
                else:
                     if payment_method  == 1:
                        input("Please enter your gift voucher code")
                        print("Thank you")
    
                     elif payment_method == 2:
                        input("Please enter your Credit / Debit card information")
                        print("Thank you")
    
                     elif payment_method == 3:
                         input("vffsdfsdfsd")
                         print("TA")
    
                     else:
                         print("Please select a valid payment method")
                         break
  • Related