0

I am a beginner. I am creating a very basic calculator.

I want to break the program when the user gives the command as "STOP". Here's my code:

while d=="yes" or d=="YES" or d=="Yes":
    a=input("Enter: A for additon, B for Subtraction, C for Multiplication, D for Division: ")
    b=int(input("Enter the first number: "))
    c=int(input("Enter the second number: "))
    if a=="A":
        print(b+c)
    elif a=="B":
        print(b-c)
    elif a=="C":
        print(b*c)
    elif a=="D":
        print(b/c)
    elif a=="STOP":
        break

I tried typing "STOP" but the output comes like this:

Do you want to calculate more? YES
Enter: A for additon, B for Subtraction, C for Multiplication, D for Division: B
Enter the first number: 46
Enter the second number: 23
23
Enter: A for additon, B for Subtraction, C for Multiplication, D for Division: STOP
Enter the first number: 1
Enter the second number: 23

After it receives the input "STOP", it asks for the first and the second number again which are the b and c variables. Either it should ask again to calculate more, or it should terminate upon the user's request. How can I make the changes??

1
  • 1
    You unconditionally ask for input of the a and b values before testing a for 'STOP' Commented Sep 24, 2023 at 18:06

1 Answer 1

0

Welcome to Stack Exchange. You are so very close with your code. You need to test for the "STOP" command before you call input() for the two numbers. If you rearrange your code like this

while d=="yes" or d=="YES" or d=="Yes":
    a=input("Enter: A for additon, B for Subtraction, C for Multiplication, D for Division: ")
    if a=="STOP":
        break
    b=int(input("Enter the first number: "))
    c=int(input("Enter the second number: "))
    if a=="A":
        print(b+c)
    elif a=="B":
        print(b-c)
    elif a=="C":
        print(b*c)
    elif a=="D":
        print(b/c)

You should get the behavior you expect.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help!!! It worked out well!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.