DJ_JONES
DJ_JONES

Reputation: 11

if-else statement and code exit

basically I'm quite new to python so I decided to make a simple calculator, I have done the coding for the calculator and that all works and I am happy, however I would like an if-else statement to see if they would like to continue with another calculation. So this is the top part of my code and the bottom part of my code, I would like to know how to get it so that after the 'else' part of the code, it just runs the rest of the code.

import os
done = ("")
if done == True:
    os._exit(0)
else:
    print ("---CALCULATOR---")

...

done = str(input("Would you like to do another calculation? (Y/N) "))
if done == "N" or "n":
    done = True
if done == "Y" or "y":
    done = False

Any help would be appreciated.

Upvotes: 1

Views: 1156

Answers (2)

Octopus
Octopus

Reputation: 8325

You'll want something like this...

import os
done = False

while not done:
    print ("---CALCULATOR---")
    ...

    # be careful with the following lines, they won't actually do what you expect
    done = str(input("Would you like to do another calculation? (Y/N) "))
    if done == "N" or "n":
        done = True
    if done == "Y" or "y":
        done = False

Upvotes: 2

MxLDevs
MxLDevs

Reputation: 19506

if done == "N" or "n":

The above condition checks whether done == "N" or "n". This will always evaluate to True because in Python, a non-empty string evaluates to boolean True.

As suggested in the comments you should use a while loop to have the program continue to execute until the user types in "N" or "n".

import os
finished = False

while not finished:
    print ("---CALCULATOR---")
    ...

    done = str(input("Would you like to do another calculation? (Y/N) "))
    if done == "N" or done == "n":
        finished = True

Upvotes: 2

Related Questions