0

I'm currently writing a program for a school project.

The purpose is for it to work as an online cash register.

answer = "a"
qty1 = 0


while True:
    answer = str(input("\nDo you really wish to buy this? (Y/N) "))
    if answer == "Y" or "y":
        qty1 = int(input("\nHow much quantity of this item would you like to buy? "))
        print("\nDo you really wish to buy", qty1, "pieces? (Y/N) ")
        answer = str(input(""))
        if answer == "Y" or "y":
            print("Confirming order and returning to menu.")
            break
        else:
            qty1=0
            print("Cancelling order and returning to menu")
            break
        
    elif answer == "N" or "n":
        print("Okay, returning to menu.")
        break
    else:
        print("not valid answer")
        
        

Here is the code for the part of the program I'm having trouble with.

Whenever I reach this part of the program, the input seems to ignore whatever I put and it always goes through the if path.

Does anyone know why this is?

I'm new to programming, so sorry if this is just an easy fix.

Kasoy
  • 11
  • 3

1 Answers1

0

Here's the working code which needed some formatting:

Changed these lines -> answer in ("Y", "y") and answer in ("N", "n"). This is because if answer == 'Y' or 'y' is used then 'y' will always evaluate to True which means the first if statement always executes. Also, the error in the print statement has a colon: print: - this was corrected.

answer = "a"
qty1 = 0


while True:
    answer = str(input("\nDo you really wish to buy this? (Y/N) "))
    print(answer)
    if answer in ("Y", "y"):
        qty1 = int(input("\nHow much quantity of this item would you like to buy? "))
        print("\nDo you really wish to buy", qty1, "pieces? (Y/N) ")
        answer = str(input(""))
        print(answer)
        if answer in ("Y", "y"):
            print("Confirming order and returning to menu.")
            break
        else:
            qty1=0
            print("Cancelling order and returning to menu")
            break
        
    elif answer in ("N" ,"n"):
        print("Okay, returning to menu.")
        break
    else:
        print("not valid answer")
        
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Devang Sanghani
  • 731
  • 5
  • 14
  • 2
    nit: It is more Pythonic to use `if answer in ("Y", "y")` instead. It would also benefit the OP and future users that come here to *explain* what was wrong and how you corrected it. Simply providing an answer is not useful to anyone but the OP. We are here to build a community and archive of answers that long transcend after we pass on :). – rayryeng Feb 16 '22 at 07:10
  • Edited the code – Devang Sanghani Feb 16 '22 at 07:11