1

I'm currently learning how to program in Python (3.8) and I've had a problem with a function in my budget tracker program. The except clause isn't executing when i type other thing then 'Day','Week','two weeks','three weeks','Month','three months','half a year','Year','two years','five years' it just continues and keep going saying 'When do you get net money?' when i type 'Day','Week'... it breaks but I want that the except clause execute when an error occurs. Thank you in advance for answering my question and making my function more efficient. If you know how to make better function to ask 'When do you get net money?' write it. Sorry if I type something wrong here, my English isn't perfect.

class Main:

    def __init__(self):
        self.income_phase = ''

    def income_phase_ask(self):
        while self.income_phase not in ['Day','Week','two weeks','three weeks','Month','three months','half a year','Year','two years','five years']:
            try:
                self.income_phase = input('When do you get net money? (Day; Week; two weeks; three weeks; Month; three months; half a year; Year; two years; piec lat): ')
            except Exception:
                print('Error! Try again!')
3
  • 5
    If the user types something not in your list, that is not an exception. Commented May 4, 2020 at 16:18
  • Nothing in your code throws an error. Why do you think it does? Commented May 4, 2020 at 16:32
  • You aren't checking for an exception. Try a = ('valid', 'inputs') and if self.income_phase not in a: raise ValueError() to trigger the except block. Commented May 4, 2020 at 21:06

2 Answers 2

1

Your code is only going to throw an exception if the instruction in the try block encounters an error. The way your code works is that as long as the user does not input the expected string, it will keep on asking.

I would also advise you to use constants to store predefined values like your list of inputs. Adding a \n at the end of your message to the user in the prompt will add a line break and make things more readable.

You don't really need to throw an Exception either, in my view. But that's up to you.

What you need is something like:

class Main:
    VALID_USER_INPUTS = ['Day','Week','two weeks','three weeks','Month','three months','half a year','Year','two years','five years']

    def __init__(self):
        self.income_phase = ''

    def income_phase_ask(self):

      self.income_phase = input('When do you get net money? (Day; Week; two weeks; three weeks; Month; three months; half a year; Year; two years; piec lat): \n')

      if self.income_phase not in self.VALID_USER_INPUTS:
        print('Error! Try again!')
        self.income_phase_ask()
Sign up to request clarification or add additional context in comments.

Comments

0

If the user types something that isn't in your list, then that is not an exception.

You could use an assert statement.

This would be your code:

try:
    assert self.income_phase not in ['Day','Week','two weeks','three weeks','Month','three months','half a year','Year','two years','five years']
except AssertionError:
    self.income_phase = input('When do you get net money? (Day; Week; two weeks; three weeks; Month; three months; half a year; Year; two years; piec lat): ')
else:
    print("Try again!")

Hopefully this helps!

4 Comments

assert statements should not be used for control flow. They are removed if Python is run in optimised mode (-O). Relying on assert and/or AssertionError for correctness will result in different behaviour of code.
@MisterMiyagi Not neccesarily. This doesn't make my code wrong, or incorrect in any way.
And that certainly doesn't mean my answer gets to be downvoted, because people who come here in the future may be looking how to use assert statements
assert statements are removed in -O mode; that is the entire point. See the assert statement and -O flag specification for reference. This code will not behave correctly in -O mode, and should not be used as a reference on how to use assert statements. Also, be aware the control flow is undesirable even in the regular case–input triggers if phase is a desired value, and print("Try again!") triggers when not asking for input.

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.