0

I have been tasked with reading values inputted by a user(using a while loop) to then store them in a list/array whilst using try: except: to determine if a given input is invalid. In continuation, if the user inputs "done" as a value it will break the loop and print() the total, sum, and average of all the imputed values.

I have gotten this snippet so far:

class Input:
      def __init__(self, number_input_value, total_to_be_calculated, main_value):
            self.number_input_value = 0
            self.total_to_be_calculated = 0.0
            self.main_value = input('Enter A Number: ')
            self.number_input_value1 = float(self.main_value)

      def loop_get_inputs(self):
            while True:
                  self.main_value
                  if self.main_value == 'done':
                        break
                  try :
                        self.number_input_value1
                  except :
                        print('INVAL["VAL"]')
                        continue
                  self.number_input_value = self.number_input_value1
                  self.total_to_be_calculated = self.total_to_be_calculated + self.number_input_value1

            print ("Finished successfully!")
            print (
              self.total_to_be_calculated,
              self.number_input_value,
              self.total_to_be_calculated/self.number_input_value
            )

if __name__ in '__main__':
      Input
      

I have no clue what's wrong, because when it runs it outputs nothing.

Output:

>>>
1
  • 1
    It's a good question for someone who's learning, but also a sign you're missing some fundamentals. Do you have a good Python reference book or resource you can turn to? This is critical for learning. Tutorials and quick introductions have many gaps. Commented Nov 18, 2020 at 23:24

2 Answers 2

1

You need create an instance of the class 'Input' and call the method:

##(self, number_input_value, total_to_be_calculated, main_value)

inp = Input(100, 1000, 10) 
#call the method
inp.loop_get_inputs()
Sign up to request clarification or add additional context in comments.

Comments

0

Basically:
1 - You have to initialize your class/object before using it.
2 - Having code on the construct is not recommend. You should call a public method of the class to start the "process".
3 - That try-except wasn't doing much. You can, for example, surround the string (from input()) cast to float and print INVALID if the input can't be casted.
4 - You can use += to simplify a = a + b
5 - lower() will convert user input to lowercase, meaning that DONE, done and DoNe (etc) will be considered as "quit" input.

Does this make sense?

class Input:
    def __init__(self):
        self.number_inputs = 0
        self.total = 0.0

    def run(self):
        self.__get_user_values()
        print(f"total: '{self.total}'")
        print(f"number_inputs: '{self.number_inputs}'")
        print(f"average: '{self.total / self.number_inputs}'")

    def __get_user_values(self):
        while True:
            value = input('Enter A Number: ')
            if value.lower() == 'done':
                break

            if self.__is_valid_input(value):
                self.total += float(value)
                self.number_inputs += 1

    def __is_valid_input(self, value) -> bool:
        try:
            float(value)
            return True
        except ValueError:
            print('INVAL["VAL"]')
            return False


if __name__ in '__main__':
    input_wrapper = Input()
    input_wrapper.run()

1 Comment

Yah this is great

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.