0

I would like to add the option of allowing the user to terminate the following program with a string command (such as 'exit' or 'quit'), but since the user input accepts and processes integers only, I can see no way to add that string exit command. For the moment, I've figured out how to add an integer exit command ("00").

(As long as I'm asking this question, my first on this forum, technical question: How do I copy-and-past code so that it looks exactly as it appears on my Idle compiler screen? I wanted to indent the body of the function definition.)

def balance_finder(amount):
if amount < 0:
    amount -= 10;

elif amount == 0:
    amount -= 5;

elif (amount > 0 and amount < 500):
    amount -= 1;

elif (amount > 500 and amount < 1000):
    amount += int(amount / 100);

else:
    amount += int(amount / 100) * 2;

print ("Your balance is: ", amount);

done = False;
while not done:
    amount = int(input("Please enter your balance (or type '00' to exit): "));
    if amount == 00:
        done = True;
        print ("Goodybye.");
    else:
        balance_finder(amount);
6
  • 1
    You do not need to end statements with semicolons in a Python script. Commented Aug 24, 2015 at 20:35
  • 1
    Paste the code directly into the box, highlight it, then hit Ctrl+K. Also, if you cast the result of input() to int, you're always going to get an int. What's the issue? Commented Aug 24, 2015 at 20:36
  • Not relevant here, but might lead to some hard to understand errors in other places: Note that 00 is octal! 00 is just 0, but e.g. 015 is 13 and 09 is a syntax error. Commented Aug 24, 2015 at 20:39
  • Re. semicolons -- old habits die hard. :) Commented Aug 24, 2015 at 20:43
  • The issue is that even without casting the result, I still can't get a string exit command to compile. Commented Aug 24, 2015 at 20:45

2 Answers 2

1

Simply check for whether it's an integer, and base the action on that:

while not done:
    amount = input("Please enter your balance (or type 'quit' to exit): ")
    try:
        amount = int(amount)
    except ValueError: # couldn't convert to integer
        done = True
        print("Goodbye.")
    else: # no error when converting to integer
        balance_finder(amount)

The loop will end on any input that isn't an integer, such as '', 'quit', or '3.9'.

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

Comments

1

Before converting the input to an int, check for 'exit' or 'quit'.

the_input = input("Please enter your balance (or type 'exit' to exit): ")
if the_input == 'exit' or the_input == 'quit':
    done = True
    print ("Goodybye.")
else:
    amount = int(the_input)
    balance_finder(amount)

3 Comments

Yes, that works, thank you. Just adding as the last line of code: balance_finder(amount)
This code will cause an error as-is. Please fix the indentation.
Thanks @TigerhawkT3. Corrected indentation, and added the last line of code.

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.