-4

I want to print "yes" when the user writes "y" and print"no" when the user writes "n" in "Are you sure for the exit" question. And the second problem is; if I write any letter instead of "y" or "n", the code is still running. How to fix it?

residuary = 1000

while True:

    operation = input("Select operation: ")

    if(operation == "q"):
        print("Are you sure for exit? (y/n)")
        answer = input("Answer:")
        y = "yes"
        n = "no"
        if(answer == "y"):
            print("See you again ")
            break
        else:
            continue
    elif(operation== "1"):
        print("Residuary is ${} .".format(residuary))
    elif (operation== "2"):
        amount = int(input("Amount you want to invest: "))
        residuary += amount
        print("${} sent to account.".format(amount))
        print("Available Residuary ${} ".format(residuary))
    elif (operation == "3"):
        amount = int(input("Amount you want to withdraw: "))
        if(amount > residuary):
                print("You can not withdraw more than available residuary!")
                continue
        residuary -= amount
        print("${} taken from account.".format(amount))
        print("Available Resiaduary ${} ".format(residuary))
    else:
        print("Invalid Operation!")
martineau
  • 119,623
  • 25
  • 170
  • 301
  • You can solve the "still running" problem with [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – martineau Mar 24 '19 at 11:36
  • Your problem statement seems unclear, and as per your second problem your input is represented in the form of string so it will accept 1 as "1" and will keep on running until proper constraints are established in your code. – Abhiram Satputé Mar 24 '19 at 11:41
  • To print "yes" as soon as the user types "y" requires getting individual keystrokes as they are typed, which is OS dependent. There are some third-party modules out there that allow this, see [Detect in python which keys are pressed](https://stackoverflow.com/questions/694296/detect-in-python-which-keys-are-pressed). – martineau Mar 24 '19 at 11:46

2 Answers2

0

Your question is not very clear. You say I want to print "yes" when the user writes "y" and print"no" when the user writes "n" in "Are you sure for the exit" question. but this line would have printed by the time you collect user wish using input("Answer:") statement.

Are you after something like following code snippet?

if(operation == "q"):
    quit = False
    while(True):
        print("Are you sure you want to exit? ([y]es/[n]o)")
        answer = input("Answer:")
        if(answer.lower() == 'y': #You may check startswith() as well
            quit = True
            print('You chose yes')
            break
        elif(answer.lower() == 'n':
            print('You chose no')
            break
    if quit:
        print("See you again ")
        break
else:
    continue
Gro
  • 1,613
  • 1
  • 13
  • 19
  • Now I got my answer, thank you for it. But I've just started to learn Python and I am at the beginning now... I haven't learned the ".lower" So you say there is no way other than this, right? – Furkan Pirci Mar 24 '19 at 11:52
-1

You just can add print statement under the condition of the printing:

    if(answer == "y"):
        print(y)
        print("See you again ")
        break
    elif (answer == "n"):
        print(n)
        continue
    else:
        break

adding else: break will exit the loop in case of the insertion of any other inout rather than y and n.

Ahmed Hawary
  • 461
  • 4
  • 15